OBJ-C: Properties

// if we want to use auto-synthesize

@interface ClassName : NSObject
{
@private
    int variableName;
}
@property (nonatomic) int variableName;
@end

@implementation ClassName
@synthesize variableName;
@end

// if we want to manually make accessors

@interface ClassName : NSObject
{
@private
    int variableName;
}
@property (nonatomic) int variableName;
- (int) variableName;
- (void) setVariableName : (int) param;
@end

@implementation ClassName
@dynamic variableName;

- (int) variableName
{
    return variableName;
}

- (void) setVariableName : (int) param
{
    variableName = param;
}

@end

NOTE:
This allows the user to use
1. instance.variableName = x;
2. x = instance.variableName;
Which is normally NOT allowed because variableName was declared private

instance->variableName is only allowed if
1. instance is a pointer
2. variableName was declared public (does not go through accessors)

In either case, users can use the methods directly due to @synthesize
1. [instance setVariableName:x];
2. x = [instance variableName];

No comments: