Underscore before property name, in setter [duplic

2019-09-24 16:14发布

This question already has an answer here:

I wrote a setter method -

- (void)setMyProp:(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?

1条回答
叛逆
2楼-- · 2019-09-24 16:32

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

@synthesize myProp=_myProp;

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

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

查看更多
登录 后发表回答