Objective-C has instances that are the objects you create and use, and there are (half-hidden) objects that are class objects that are created by the compiler. The class object where the methods for the class are stored; each instance contains only its own data (i.e. instance variables).
Bob * myBob = [[Bob alloc] init];
Here myBob is an instance. Each instance refers to its class object. 1 When you call a method on an instance:
[myBob frogBlastTheVentCore]
the runtime goes and searches for the method in the class object, and then uses the instance and instance data to execute the method. That the main organization of objects in Obj-C: instance objects stores data and has links to their class objects that contain methods. There is only one class object in a class; all instances of this class have a reference to the same class object.
A class (viewed as a "type" rather than an object for a moment 2 ) is said to be inherited from another class:
@interface Bob : NSObject { NSColor * uniformColor; } + (BOOL) willShootAtPlayer; - (void) frogBlastTheVentCore; @end @interface VacuBob : Bob {} @end
Here VacuBob is a subclass of Bob ; any VacuBob instance has its own uniformColor instance uniformColor . Similarly, an object of the VacuBob class created by the compiler is created; it also inherits from Bob - from an object of class Bob . This means that the VacuBob class VacuBob also has a willShootAtPlayer method.
In the line you posted:
... aString = [NSString alloc] ...
the class object is actually NSString here. You call the class method with the name +[NSString alloc] 3 (class methods are denoted by + , not - 4 ). When the class name is used as the recipient of the message (the first half of the expression in parentheses), it refers to an object of class 5 . In this case, both NSString and aString are objects; these are just two different objects; aString is an instance.
Dave DeLong is connected with good mail on this (the chart, in particular, pretty much gives everything out); for more information, you should also read Matt Neuberg's description of The Secret Life of Classes in his iOS book. It describes the creation of class objects, their use, and other actions that they perform, in addition to retention methods.
1 This isa pointer: myBob->isa refers to an object of class Bob .
2 The variable related to the class object is of type Class . The type of the instance object is its class. Thus, the type Bob is equal to Class , and the type myBob is Bob * (that is, a pointer to an object of type Bob ). The distinction between the type of a variable and the type of an object can cause some confusion here.
3 The return value of alloc is an NSString instance that you invoke the initWithString: instance initWithString:
4 The methods of the parallel instance class are that they are called with the class object itself as an argument. Since class objects do not have their own data, the use of class methods is perhaps more limited than other OO languages; class methods are most often used for vending instances.
5 When it is used in a variable declaration: NSString * mySting; is the name of the variable type.