“获取” Objective-C中@property声明中的关键字? [英] "getter" keyword in @property declaration in Objective-C?

查看:69
本文介绍了“获取” Objective-C中@property声明中的关键字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我注意到苹果文档中的一些代码示例在声明属性时显示了以下样式:

I noticed some code example in Apple's documentation shows the following style when declaring the property:

@property (nonatomic, getter=isActivated) BOOL activated;

我知道它允许您为getter方法指定一个特定的名称。我想知道使用这种样式的原因和优势。

I understand it allows you to specify a certain name for your getter method. I'd like to know what is the reason and advantage to use this style.

我将能够使用点符号来获取值(例如BOOL aBool = someObject.isActivated)吗?还是应该使用

[someObject isActivated];
来访问属性?谢谢!

Will I be able to use the dot notation to get the value (e.g. BOOL aBool = someObject.isActivated)? Or should I use
[someObject isActivated]; to access the property? Thanks!

推荐答案

否, getter 关键字仅更改方法名称。想法是,您将像访问变量一样访问属性:

No, the getter keyword only changes the method name. The idea is that you'll access the property just like a variable:

if (self.activated) { ... }
self.activated = YES;

但是当您发送消息到对象时,它是可读的代码: if([self isActivated]){...}

But when you're sending a message to the object, it's readable code: if ([self isActivated]) { ... }.

这篇关于“获取” Objective-C中@property声明中的关键字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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