ios 使用 BOOL 属性
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4864239/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using a BOOL property
提问by Patrick
Apple recommends to declare a BOOL property this way:
Apple 建议以这种方式声明 BOOL 属性:
@property (nonatomic, assign, getter=isWorking) BOOL working;
As I'm using Objective-C 2.0 properties and dot notation, I access this property using self.working
. I know that I could also use [self isWorking]
— but I don't have to.
由于我使用的是 Objective-C 2.0 属性和点表示法,因此我使用self.working
. 我知道我也可以使用[self isWorking]
- 但我不必。
So, as I'm using dot notation everywhere, why should I define an extra property? Would it be okay to simply write
所以,当我到处使用点表示法时,我为什么要定义一个额外的属性?简单地写可以吗
@property (nonatomic, assign) BOOL working;
Or do I have any benefits writing getter=isWorking
in my case (usage of dot notation)?
或者我getter=isWorking
在我的案例中写作有什么好处(点符号的使用)?
Thanks!
谢谢!
回答by BoltClock
Apple simply recommends declaring an isX
getter for stylistic purposes. It doesn't matter whether you customize the getter name or not, as long as you use the dot notation or message notation with the correct name. If you're going to use the dot notation it makes no difference, you still access it by the property name:
Apple 只是建议isX
出于风格目的声明一个getter。是否自定义 getter 名称并不重要,只要您使用正确名称的点符号或消息符号即可。如果您打算使用点表示法,它没有任何区别,您仍然可以通过属性名称访问它:
@property (nonatomic, assign) BOOL working;
[self setWorking:YES]; // Or self.working = YES;
BOOL working = [self working]; // Or = self.working;
Or
或者
@property (nonatomic, assign, getter=isWorking) BOOL working;
[self setWorking:YES]; // Or self.working = YES;, same as above
BOOL working = [self isWorking]; // Or = self.working;, also same as above
回答by Hariprasad.J
Apple recommends for stylistic purposes.If you write this code:
Apple 建议用于文体目的。如果您编写此代码:
@property (nonatomic,assign) BOOL working;
Then you can not use [object isWorking].
It will show an error. But if you use below code means
那么你就不能使用 [object isWorking]。
它会显示错误。但是如果你使用下面的代码意味着
@property (assign,getter=isWorking) BOOL working;
So you can use [object isWorking] .
所以你可以使用 [object isWorking] 。
回答by Thomson Comer
There's no benefit to using properties with primitive types. @property
is used with heap allocated NSObjects
like NSString*
, NSNumber*
, UIButton*
, and etc, because memory managed accessors are created for free. When you create a BOOL
, the value is always allocated on the stack and does not require any special accessors to prevent memory leakage. isWorking
is simply the popular way of expressing the state of a boolean value.
使用具有原始类型的属性没有任何好处。 @property
与NSObjects
像NSString*
、NSNumber*
、UIButton*
等分配的堆一起使用,因为内存管理访问器是免费创建的。创建 时BOOL
,该值始终在堆栈上分配,并且不需要任何特殊访问器来防止内存泄漏。 isWorking
只是表达布尔值状态的流行方式。
In another OO language you would make a variable private bool working;
and two accessors: SetWorking
for the setter and IsWorking
for the accessor.
在另一种 OO 语言中,您将创建一个变量private bool working;
和两个访问器:SetWorking
用于设置器和IsWorking
访问器。