开发者

Can boolean properties' accessors be synthesized according to the coding guidelines (ex: isEnabled getter)?

I've read the Coding Guidelines for Cocoa for accessor methods and it invites y开发者_如何学Goou to write getter methods for instance variables expressed as adjective (ex: enabled) as isEnabled instead of simply enabled.. is there a way to instruct the @synthesize keyword to produce such a getter or should you always write the getter method declaration and implementation by hand letting the @synthesize keyword generate only the setter method? I know they are just guidelines, but I think it is better to adhere to the behavior suggested by the official docs :)


You can do the following:

@property (nonatomic, getter=isEnabled) BOOL enabled;

This will use isEnabled as the name for the getter method.

You will just @synthesize it as normal:

@synthesize enabled;


As @Douwe Maan mentioned in his answer, "you can set the getter/setter method names in the @property declaration and just @synthesize it" to let the compiler take care of the getter and setter method definitions.

But, if you want to have your own implementations of getter or setter or both, you can very well "implement the methods in your implementation file and omit the @synthesize line".

You can "both @synthesize the property and add your own implementations of getter/setter methods", in which case compiler will just use your getter/setter method implementations without generating them.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜