Types in Objective-C on iOS
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Objective-C on iOS mixes C types, Objective-C object types, and Apple framework-specific aliases. That combination is powerful, but it also confuses newcomers because similar-looking types serve very different purposes. The most useful mental model is to separate primitive values, object references, and Cocoa convenience types such as NSInteger and CGFloat.
Primitive C Types Still Matter
Objective-C inherits its primitive types directly from C. These are value types, not objects.
These types are fast and compact, but they do not support Objective-C messaging because they are not objects.
On iOS, BOOL is the usual boolean type, with YES and NO rather than C99 true and false in most Objective-C codebases.
Object Types Are Pointers
Objective-C objects are referenced by pointers. NSString *, NSArray *, and NSObject * all point to heap-allocated objects.
The * matters. It tells you the variable stores a reference, not the object contents inline.
This is also why nil is valid for object references but not for primitive values like int or double.
Prefer Cocoa Aliases for Platform-Aware Numeric Types
Apple frameworks use types such as NSInteger, NSUInteger, and CGFloat because their size maps cleanly to the platform architecture.
Why use them instead of plain int or float?
- '
NSIntegerandNSUIntegermatch the natural integer width of the platform' - '
CGFloatmatches the floating-point type expected by UIKit and Core Graphics' - framework APIs already use these types, so matching them reduces conversion noise
For UIKit geometry, you will see structs built from these aliases:
These are structs, not objects, which is why they are passed by value.
Understand id, instancetype, and Class
Three Objective-C types come up often in APIs:
- '
idmeans "an object of some unknown Objective-C type"' - '
instancetypemeans "the concrete type returned by this initializer or factory"' - '
Classmeans "an Objective-C class object"'
Example:
instancetype is better than id for initializers because the compiler can preserve the specific return type for subclasses.
Use id when you genuinely want dynamic typing, not as a shortcut to avoid thinking about types.
Box Primitive Values When Collections Need Objects
Objective-C collections such as NSArray and NSDictionary store objects, not primitive values directly. When you need to put an int or BOOL into a collection, use NSNumber.
This boxing behavior is common in Cocoa APIs. The same idea applies to wrapping structs with NSValue when necessary.
Nullability and Modern Objective-C
Modern Objective-C code often annotates pointers with nullability:
These annotations improve Swift interoperability and make intent clearer in mixed-language iOS apps. They are especially important in public headers and framework code.
Common Pitfalls
- Treating primitive C values and Objective-C object references as if they behaved the same way.
- Using plain
inteverywhere instead of framework-friendly aliases such asNSInteger. - Returning
idfrom initializers whereinstancetypeis the correct type. - Forgetting that collections store objects, not raw primitive values.
- Ignoring nullability annotations in code that interoperates with Swift.
Summary
- Objective-C uses both C primitive types and Objective-C object pointer types.
- Framework aliases such as
NSIntegerandCGFloatare preferred for iOS APIs. - '
id,instancetype, andClasshave distinct roles and should not be used interchangeably.' - Collections require object values, so primitives are boxed with
NSNumberorNSValue. - Nullability annotations make modern Objective-C clearer and much safer in mixed Swift projects.
Related reading
- UI lags during heavy computation operation in Flutter even with async
- UIActionSheet cancel button strange behaviour
- UIActivityViewController crashing on iOS 8 iPads
- UIActivityViewController crashing on iOS 8 iPads
- UIAlertController custom font, size, color
- UIAlertView first deprecated IOS 9
- UIApplication.registerForRemoteNotifications must be called from main thread only
- UIBarButtonItem in navigation bar programmatically?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.