Sunday, May 4, 2014

Objective-C Setter and getter methods are collectively known as accessor methods, or simply accessors.

Setter and getter methods are collectively known as accessor methods, or simply accessors.

Getter methods are given the name of the instance variable minus the underscore.
// Instance variable declarations
{
    float _heightInMeters;
    int _weightInKilos;
}

// Getter method declarations
- (float)heightInMeters;
- (int)weightInKilos;
Setter methods start with set followed by the name of the instance variable minus the underscore. Notice that the case of the setter’s name adjusts to preserve the camel-casing. Thus, the first letter of the instance variable name is uppercase in the setter’s name.
// Setter method declarations - notice difference in casing!
- (void)setHeightInMeters:(float)h;
- (void)setWeightInKilos:(int)w;

No comments:

Post a Comment