Cocoa Touch
Everything the hiker actually touches in TrailLog — the list of hikes, the "Log hike" button, the swipe to delete, the reminder notification — comes from the top layer of iOS. Cocoa Touch is where your app meets the person holding the phone.
The rule
Cocoa Touch turns the layers below into screens, touches and app life.
The top layer holds the frameworks that make an app feel like an iPhone app:
- UIKit — views, view controllers, controls, navigation, text input, and the event loop that delivers touches. It also manages the app lifecycle: launching, going to the background, coming back.
- SwiftUI — Apple's newer declarative UI framework, built alongside UIKit (and in places on top of it).
- User Notifications — local and push notifications.
- Gesture recognisers — taps, swipes, pinches and long presses turned into simple callbacks.
- Other frameworks for user-facing features: MapKit (maps), PhotosUI (photo picker), WidgetKit (home-screen widgets).
Cocoa Touch is a heavy user of the layers below. A UIButton draws with Core Graphics, animates its highlight with Core Animation, and stores its title as a Foundation String.
import UIKit
let logButton = UIButton(type: .system)
logButton.setTitle("Log hike", for: .normal)
logButton.addTarget(self, action: #selector(logHike), for: .touchUpInside)That one import UIKit also brings in Foundation, which is why most UIKit files need no separate import Foundation.
A key rule of this layer: UI work happens on the main thread. UIKit is not thread-safe, so every change to a view must be made from the main thread, which also runs the event loop.
The shortcut
Ask: "Does the user see it or touch it?" If yes, start in Cocoa Touch. If not, look lower.
A "Log hike" button
The user touches it → Cocoa Touch (UIKit's
UIButtonor a SwiftUIButton).
The trap is putting non-visual work, like parsing a big file, into a view controller on the main thread. The layer rule and the main-thread rule together say: keep Cocoa Touch for what the user sees, and do slow work elsewhere.
Drills
Answer fast — trust the rule.
Which layer contains UIKit?
A daily hiking reminder appears on the lock screen. Which framework schedules it?
On which thread must you update a UILabel's text?
Which statement about Cocoa Touch and the lower layers is true?
Remember
- Cocoa Touch is the top layer: UIKit, SwiftUI, notifications, gestures and the app lifecycle.
- Shortcut: if the user sees or touches it, start in Cocoa Touch.
- Trap: UI changes only on the main thread — keep slow, invisible work out of it.
