@property setter for BOOL

Geo*_*rge 3 boolean properties objective-c

我在使用@property和设置BOOL时遇到问题@synthesize.我正在使用@property BOOL isPaused;而且我可以通过使用获得它,[myObject isPaused];但我无法设置它.我想用[myObject setPaused: NO];.我也试过,@property (setter=setPaused) BOOL isPaused;但如果我没有误会,那么我需要自己写那个二传手.

ken*_*ytm 6

为什么不使用点符号?

myObject.isPaused = YES;
return myObject.isPaused;
Run Code Online (Sandbox Code Playgroud)

如果您的属性声明为@property BOOL isPaused,则始终将setter称为

[myObject setIsPaused:YES];
Run Code Online (Sandbox Code Playgroud)

要重命名setter,您必须提供包括冒号的完整签名:

@property(setter=setPaused:) BOOL isPaused;
...
[myObject setPaused:YES];
Run Code Online (Sandbox Code Playgroud)

顺便说一句,命名约定不包括属性中的动词.

@property(getter=isPaused) BOOL paused;
Run Code Online (Sandbox Code Playgroud)

  • 实际上,在这种情况下,"isPaused"是正确的命名约定.http://developer.apple.com/mac/library/DOCUMENTATION/Cocoa/Conceptual/CodingGuidelines/Articles/NamingMethods.html#//apple_ref/doc/uid/20001282-1004202-BCIGGFCC (5认同)