Martin R
Martin R

Reputation: 539685

How to use custom getter method in Objective-C class

I noticed that for a property with a custom getter method

@interface MyClass : NSObject
@property (nonatomic,getter=isActive) BOOL active;
@end

both

BOOL b = myObj.isActive

and

BOOL b = myObj.active

can be used to get the value. In both cases the isActive method is called.

Upvotes: 3

Views: 976

Answers (1)

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726479

There is no difference between the two. You use a custom name when you want to break the established convention of XYZ+setXYZ in cases when the alternative names make more sense from the point of view of English grammar. For example,

if ([myRobot isActive]) {
    ....
}

reads better than

if ([myRobot active]) {
    ....
}

You could have declared your property as isActive, but then your setter would be setIsActive, which sounds slightly worse than setActive.

Upvotes: 9

Related Questions