Objective-C Syntax and Variables
Objective-C syntax is C syntax extended with Smalltalk-style object messaging, so every valid C statement, loops, if/else, arithmetic, pointers, is also valid Objective-C, and object-oriented additions like @interface, @implementation, and @property are layered on top using an @ prefix that makes them instantly recognizable in source code. A typical class is declared across two files: a header (.h) exposing the public interface and an implementation (.m) containing the actual method bodies.
Cricket analogy: Like Test cricket keeping all of first-class cricket's basic rules, overs, wickets, boundaries, while adding its own five-day format on top, Objective-C keeps all of C's rules while adding @ prefixed object syntax on top.
Declaring Variables
Primitive variables in Objective-C are declared exactly as in C, int count = 0;, float ratio = 1.5f;, char letter = 'A';, while object variables are declared as pointers to a class, such as NSString *name = @"Ada"; or Person *p = [[Person alloc] init];, because every Objective-C object lives on the heap and is accessed only through a pointer. Under Automatic Reference Counting (ARC), the compiler inserts the necessary retain and release calls automatically, so developers rarely manage that memory by hand.
Cricket analogy: Like a scoreboard operator tracking the simple run total directly (an int) but tracking each individual player's full profile through a separate roster sheet (a pointer to a record), Objective-C stores primitives directly but stores objects through pointers.
Interface and Implementation Files
The header file declares a class's public shape using @interface Person : NSObject ... @end, listing properties and method signatures that other files can #import and use, while the implementation file opens with @implementation Person and provides the actual method bodies, closing with @end. Objective-C uses #import instead of C's #include specifically because #import automatically guards against a header being processed twice, eliminating the need for manual include guards.
Cricket analogy: Like a team sheet handed to the umpires listing only player names and batting order (the public interface) while the actual training ground routines stay private with the coaching staff (the implementation), @interface exposes what's public and @implementation hides how it's done.
// Person.h
#import <Foundation/Foundation.h>
@interface Person : NSObject
@property (nonatomic, copy) NSString *name;
@property (nonatomic, assign) NSInteger age;
- (instancetype)initWithName:(NSString *)name age:(NSInteger)age;
- (void)printBio;
@end
// Person.m
#import "Person.h"
@implementation Person
- (instancetype)initWithName:(NSString *)name age:(NSInteger)age {
self = [super init];
if (self) {
_name = [name copy];
_age = age;
}
return self;
}
- (void)printBio {
NSLog(@"%@ is %ld years old.", self.name, (long)self.age);
}
@endBy convention, Objective-C class names are PascalCase and are often prefixed with two or three letters to avoid namespace collisions, NS for Foundation/AppKit classes inherited from NeXTSTEP, UI for UIKit, and a custom prefix like AB or MYC for your own app's classes, since Objective-C has no built-in namespace system like Swift's modules.
Objective-C distinguishes nil (a null object pointer), Nil (a null class pointer), and NULL (a null C pointer), they are not strictly interchangeable in older non-ARC code, and confusing them, or forgetting a semicolon at the end of a statement inherited from C syntax, are two of the most common syntax mistakes for developers coming from garbage-collected languages.
- Objective-C syntax is C's syntax plus @-prefixed keywords like @interface, @implementation, and @property.
- A class is typically split across a .h header (public interface) and a .m implementation file.
- Primitive variables (int, float, char) are declared exactly as in C.
- Object variables are always declared as pointers, e.g. NSString *name, because objects live on the heap.
- ARC automatically inserts retain/release calls, removing most manual memory management.
- #import guards against double-processing a header automatically, unlike C's #include.
Practice what you learned
1. Which keyword pair declares a class's public interface in Objective-C?
2. Why are Objective-C objects always accessed through pointers?
3. What is the key advantage of #import over C's #include in Objective-C?
4. What does ARC stand for and what does it do?
5. What is the conventional purpose of a two- or three-letter class name prefix like NS or UI?
Was this page helpful?
You May Also Like
What Is Objective-C?
A history and overview of Objective-C, the C-based, Smalltalk-inspired language that powered Apple's platforms for decades.
Objective-C Data Types
Objective-C's primitive types, Foundation typedefs like BOOL and NSInteger, and object types accessed through pointers.
Your First Objective-C Program
Writing, compiling, and running a first Objective-C 'Hello, World!' program from the terminal.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics