100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
Programming

Objective-C Syntax and Variables

How Objective-C extends C syntax with object messaging, and how primitive versus object variables are declared.

FoundationsBeginner9 min readJul 10, 2026
Analogies

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.

objectivec
// 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);
}

@end

By 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

Was this page helpful?

Topics covered

#Programming#ObjectiveCStudyNotes#ObjectiveCSyntaxAndVariables#Objective#Syntax#Variables#Declaring#StudyNotes#SkillVeris#ExamPrep