下划线在属性名称之前,在setter中 [英] Underscore before property name, in setter

查看:126
本文介绍了下划线在属性名称之前,在setter中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一个setter方法 -

I wrote a setter method -

- (void)setMyProp:(MyProp *)myProp{
    _myProp = myProp;
}

属性名称如何工作?
我知道这个问题已经提出,但他们是关于用户设置属性名称为_myProp,一些约定。我不是合成或更改属性名称。

How is underscore put before property name is working? I know this question has been asked, but they are about user setting property name to _myProp, some convention. I am not synthesizing or changing the property name. How this underscore is working?

推荐答案

如果您使用的是最新版本的LLVM,那么编译器将为您创建@synthesize语法:

If you are using latest version of LLVM, then the compiler creates @synthesize for you with the syntax:

@synthesize myProp=_myProp;

因此,即使您未明确合成,也可以使用_myprop。

Therefore you are able to use _myprop even though you have not synthesized explicitly.


* SideNote: _myProp 可以直接访问属性, self.myProp 将调用accessor。始终使用 self.myProp

*SideNote: _myProp makes you access the property directly, while self.myProp will call accessor. Always use self.myProp

这篇关于下划线在属性名称之前,在setter中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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