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

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

问题描述

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

Apple recommends to declare a BOOL property this way:

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

因为我使用的是 Objective-C 2.0 属性和点符号,所以我使用 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 名称无关紧要,只要使用正确名称的点符号或消息符号即可.如果您打算使用点表示法,它没有任何区别,您仍然可以通过属性名称访问它:

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天全站免登陆