Instance vs. Class Methods

Instance Class
Method called on an instance of a class, an object. Method called on the class itself, a static method.
A “normal” method, that affects a specific object. Conventially used for object creation and utilities.
Starts with a -, for example:
- (void)addWeight:(double)weightToAdd;
- (id)initWithFormat:(NSString *)format ...
Starts with a +, for example:
+ (id)alloc
+ (id)stringWithFormat:(NSString *)format, ...




nil is equivalent to NULL is equivalent to 0.

nil is the default value of an object pointer. All instance variables start out set to 0.

You can test for nil in an if statement. This is useful to check if a variable has been instantiated or not.



Object Instantiation

There are many different ways to instantiate (to create) an object.

Using class methods:

+ (id)stringWithFormat:(NSString *)format, ...

+ (id)arrayWithCapacity:(NSUInteger)numItems


Using other objects:

- (NSString *)stringByAppendingString:(NSString *)aString

- (NSString *)componentsJoinedByString:(NSString *)separator


From scratch, by allocating and initializing:

  • Allocating: Reserving enough space in memory to hold the object.
  • Initializing: Building the actual object.

- (id)init is the plain object initializer. It is declared in the base class NSObject.

We’ll take a close look at id soon, but for now remember that - (id)init returns an initialized object.

You can override and customize - (id)init in your subclasses.

Objects can have more complicated - (id)init methods. These methods must start with the init prefix and will usually have the format - (id)initWithSomething:(SomeClass *)someInstance.

Objects can also have multiple initializers.

  1. Paul Hegarty. Lecture 3 Slides. iPad and iPhone Application Development. Stanford, Nov 14 2011.

Comments are closed.