Thursday, July 9, 2009

Believe in your 'self' but be careful when assigning.

self in Objective-C is analogous to this in C++ and Java.

name = @"Jed"; // accesses the instance variable directly
self.name = @"Jed"; // access the setter method of the instance variable.

// again, from stanford course
@implementation Person
- (void)setAge:(int)newAge {
[self setAge:newAge]; // Infinite loop!
}
@end

No comments:

Post a Comment