使用BOOL属性 [英] Using a BOOL property

查看:171
本文介绍了使用BOOL属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Apple建议以这种方式声明BOOL属性:

Apple recommends to declare a BOOL property this way:

@property (nonatomic, assign, getter=isWorking) BOOL working;

由于我使用的是Objective-C 2.0属性和点符号,我使用<$访问此属性C $ C> self.working 。我知道我也可以使用 [self isWorking] - 但我没有。

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.

所以,因为我到处都使用点符号,为什么要定义一个额外的属性?可以简单地写

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;

或者我有什么好处写 getter = isWorking 在我的情况下(使用点符号)?

Or do I have any benefits writing getter=isWorking in my case (usage of dot notation)?

谢谢!

推荐答案

Apple只是建议声明 isX getter用于样式目的。无论您是否自定义getter名称都无关紧要,只要您使用带有正确名称的点表示法或消息表示法即可。如果您打算使用点符号,则没有区别,您仍然可以通过属性名称访问它:

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:

@property (nonatomic, assign) BOOL working;

[self setWorking:YES];         // Or self.working = YES;
BOOL working = [self working]; // Or = self.working;

@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

这篇关于使用BOOL属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆