开发者

Why underscores before instance variables in Objective-C? [duplicate]

开发者_如何学JAVAThis question already has answers here: Closed 11 years ago.

Possible Duplicates:

How does an underscore in front of a variable in a cocoa objective-c class work?

Prefixing property names with an underscore in Objective C

Why do a lot of Objective-C programmers put underscores before instance variables (even if they never even access them directly anywhere from their code)? What's the logic behind that?


Prepending an underscore to instance variable names is a convention for all variables that aren't meant to be accessed outside of the class instance. This makes it clear to the developer (and the user of the code if it's in a library) what's intended to be accessed externally and what isn't.


The logic is to make it clear when accessing the property vs accessing the ivar.

You can't mistakenly access the raw ivar without the property if its underscored. I don't do this in my own code though.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜