在Objective-C中加下划线的前缀变量名是什么意思? [英] What do underscore prefixed variable names in Objective-C mean?

查看:114
本文介绍了在Objective-C中加下划线的前缀变量名是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我注意到,在许多社区的Objective-C类和Apple的框架中,它们都使用约定为变量命名,这些约定使用带下划线的前缀,例如:_name.有下划线的原因是什么.我应该在自己的课堂上这样做吗?如果是这样,我应该在何时何地使用它?

I noticed that in many community Objective-C classes and in Apple's frameworks they name some of the variables using a convention that prefixes variables with an underscore, such as: _name. What is the reason for having the underscore. Should I be doing this in my own classes? If so where and when should I use it?

推荐答案

在可可粉中,惯例是表示某些东西是私有的,不应在外部使用.但是,这是非正式的惯例,尤其是考虑到

In Cocoa, it's a convention to indicate the something is private and shouldn't be used externally. However, it's unofficial convention, particularly in light of wording like this in the documentation:

以"_"(单个下划线字符)开头的方法名称保留供Apple使用.

Method names beginning with "_", a single underscore character, are reserved for use by Apple.

但是,该建议特别适用于方法,而不适用于变量.因此,如果您想在变量前加下划线,请继续.话虽如此,如果您使用下划线前缀表示某些数据的私有性质,那么也许您不应该一开始就将其公开...

However, that recommendation specifically applies to methods, not variables. So if you'd like to prefix your variables with underscores, go right ahead. That being said, if you're using the underscore prefix to indicate the private nature of some data, perhaps you shouldn't be exposing it in the first place...

这篇关于在Objective-C中加下划线的前缀变量名是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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