Pebellslearn by building
0%
RoadmapsMatrixCoursesSandboxSign in
Choosing a Language  ›  Lesson

Objective-C Basics

Objective-C Basics 12 minObjective-C is C with objects added, where you call a method by sending a message to a receiver inside square brackets.

Objective-C Basics

Before you write a line of Swift, you will meet Objective-C: in older TrailLog-style codebases, in Stack Overflow answers from 2012, and in the names of Apple's own types. NSString, NSDate, UIViewController — those prefixes come from Objective-C. You do not need to write it daily, but you do need to read it without panic.

The rule

Objective-C is C plus messages sent in square brackets.

Objective-C is a strict superset of C: any valid C code is valid Objective-C. On top of C it adds objects, and the way you talk to an object is by sending it a message inside square brackets.

// Hike.h — the interface: what a Hike offers
@interface Hike : NSObject
@property (nonatomic, copy) NSString *name;
@property (nonatomic) double distanceKm;
- (NSString *)summary;
@end

// Hike.m — the implementation: how it does it
@implementation Hike
- (NSString *)summary {
    return [NSString stringWithFormat:@"%@: %.1f km", self.name, self.distanceKm];
}
@end

Read it piece by piece:

  • Two files per class. The .h header declares the public interface; the .m file holds the code. Other files #import the header.
  • @ marks Objective-C additions to C: @interface, @property, @end, and string literals like @"Ridge Loop" (a plain "..." would be a C string).
  • * on object types. Every object is reached through a pointer: NSString *name.
  • - and +. A minus starts an instance method, a plus starts a class method.
  • Messages. [hike summary] sends the summary message to hike. With arguments, the method name is split into labelled parts: [hike setName:@"Ridge Loop"].

One surprising rule: sending a message to nil is allowed and simply returns nil or zero. Nothing crashes, which hides bugs — one of the reasons Swift was designed differently.

The shortcut

To read any Objective-C line, find the brackets, then read receiver first, message second. Nested brackets are evaluated inside out, just like nested function calls.

Reading a nested messageStep 1 of 4
  1. The line

    NSString *text = [[hike summary] uppercaseString];

A common misreading is to treat the first word inside the brackets as the method. It is always the receiver; the method comes after it.

Drills

Answer fast — trust the rule.

quick drill

In [trail addHike:newHike], which part is the receiver?

quick drill

Which file normally declares a class's public properties and methods?

quick drill

What happens when you send a message to nil in Objective-C?

quick drill

Which line is an Objective-C string object literal?

Remember

  • Objective-C is C plus objects, and you talk to objects by sending messages in square brackets.
  • Read brackets inside out: receiver first, then message; .h declares, .m implements.
  • Trap: messaging nil silently returns nil or zero instead of failing loudly.

Practice — level up

0/1 solved

Write your answer, then compare it with a rubric and a model answer.

ExplainTranslate an Objective-C line

Explain what NSString *label = [[hike name] stringByAppendingString:@" trail"]; does, piece by piece, and write the equivalent Swift line.