Thursday, September 24, 2009

Instantiation

Plus signs denote class methods, minus signs denote instance methods.
Class methods have no access to instance variables.
@implementation classname
+classMethod {
// implementation
}
-instanceMethod {
// implementation
}
@end


/////

Instantiation

Once an Objective-C class is written, it can be instantiated. This is done by first allocating the memory for a new object and then by initializing it. An object isn't fully functional until both steps have been completed. These steps are typically accomplished with a single line of code:

MyObject * o = [[MyObject alloc] init];

The alloc call allocates enough memory to hold all the instance variables for an object, and the init call can be overridden to set instance variables to specific values on creation. The init method is often written as follows:

-(id) init {
self = [super init];
if (self) {
ivar1 = value1;
ivar2 = value2;
.
.
.
}
return self;
}

////

Properties are implemented by way of the @synthesize keyword, which generates getter and setter methods according to the property declaration. Alternately, the @dynamic keyword can be used to indicate that accessor methods will be provided by other means.

No comments:

FEEDJIT Live