Jul 21

Most Object Oriented programmers cling to the Encapsulation element of the three core of Object Oriented programming more than any other. And with most languages this means you spend a lot of your time as a programmer writing getter and setter methods. With the advent of many IDE features, you can have the code generated for you, but it is still code living in your source.

Objective C provides a mechanism to ease the developer from this burden. A “property” provides shorthand to generate accessor methods at compile time.

@property (<attributes>) <type> <name>

In the implementation, you can tell it to generate the accessors using “synthesize”

@synthesize <name>

So, if we have an int X, and I really don’t feel like writing the accessor methods, my code would look like the following:

@interface MyClass : NSObject {

int x;

}

@property(readwrite) int x;

@end

@implementation MyClass

@synthesize x;

@end

This will create methods: [MyClass x] and [MyClass setX:intValue]

Tagged with:
preload preload preload