Objective-C Nullability: Bridging to Swift's Optionals

Nullability annotations are like adding ? or ! to your Objective-C pointers, telling Swift how to handle nil. They're essential in mixed codebases to bridge Objective-C's pointers to Swift's safe Optionals.
Why it exists
Objective-C pointers could always be nil, a concept handled loosely at runtime. Swift introduced strict compile-time safety around nil values with Optionals. To make these two languages work together safely, Objective-C needed a way to declare the "nullability" of its pointers for the Swift compiler.
The mental model
Think of nullability annotations as a contract with the Swift compiler. You're promising whether a pointer, property, or method return value can ever be nil. This contract allows Swift to generate safer, more idiomatic code, like String? for nullable values and String for non-null ones, instead of making dangerous assumptions.
How it works
You add keywords like nullable and nonnull to pointer types in your Objective-C header files. For example, NSString _Nullable name; tells Swift this can be nil, so it imports it as var name: String?. Conversely, NSString _Nonnull name; imports as var name: String. To reduce boilerplate, you can wrap entire sections of your header in NS_ASSUME_NONNULL_BEGIN and NS_ASSUME_NONNULL_END macros, which makes the compiler assume all pointers are nonnull unless explicitly marked nullable.
When to use it
Use them in all Objective-C header files that are exposed to Swift. This is non-negotiable for modern mixed-language iOS and macOS apps. It improves API clarity, enables better compile-time warnings, and prevents a whole class of runtime crashes by making nil-safety explicit.
When not to use it
There's almost no reason to avoid them in code that interoperates with Swift. For purely legacy Objective-C modules that will never be touched by Swift, they are less critical but still good practice for self-documentation. The _Null_unspecified annotation exists for cases where nullability is truly ambiguous, but using it opts out of safety checks and should be a last resort.
One canonical example
An Objective-C method declared as - (nullable NSString )findUserByID:(nonnull NSNumber )userID; is exposed to Swift as func findUser(byID userID: NSNumber) -> String?. The nullable return type becomes an optional String?, while the nonnull parameter remains a non-optional NSNumber, enforcing at the call site that you cannot pass nil for the user ID.
Interview question
What is the primary role of nullability annotations in Objective-C headers when the code is consumed by Swift?
- a.To force Objective-C to adopt Swift's strict memory management rules for pointers.
- b.To automatically convert all Objective-C pointers into Swift's non-optional types, simplifying code.
- c.To reduce the overall memory footprint of Objective-C objects when they are used in Swift.
- d.To provide the Swift compiler with explicit information about an Objective-C pointer's potential nil state, enabling safer type bridging.Correct
Why? this is the answer
Nullability annotations act as a contract, informing Swift whether an Objective-C pointer can be nil, which allows Swift to correctly bridge it to an Optional (Type?) or a non-optional (Type). Option B is incorrect because only _Nonnull pointers become non-optional; _Nullable pointers become Optionals.
Just read this? Test yourself on what you have been reading.
Read the original → developer.apple.com
- #ios
- #swift
- #objective-c
- #interoperability
Put your scrolling time to good use
Learn one idea, try a quiz and save useful cards for revision. Tezvyn makes it easy to learn and stay current in your tech field, a few minutes at a time.
The iPhone app is on the way
We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.
Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.
We are hiring for this. Open roles that interview on ios — each one lists the topics its interview covers.
See open roles