Objective-C:你在代码中使用@private可视性/access修饰符吗?

时间:2022-09-07 07:38:07

There are 3 modifiers: @private, @protected (default) and @public. Being accustomed to do so in C++ and other more sane languages, I always use @private for my fields. I barely (if ever)see that in SDK examples from Apple - they just rely on the default one.

有3个修饰符:@private、@protected(默认)和@public。在c++和其他更理智的语言中,我经常使用@private作为字段。我很少(如果有的话)在苹果的SDK示例中看到这一点——它们仅仅依赖于默认的那个。

One day I realized that Objective-C inheritance is rather fake feature: deriving an interface from another one doesn't mean all private fields are now available for redefinition. The compiler still sees them and disallows defining a new private field with the the same name, which goes orthogonal with classic encapsulation paradigm in OOD.

有一天我意识到Objective-C继承是相当假的特性:从另一个接口派生接口并不意味着现在所有的私有字段都可以重新定义。编译器仍然看到它们,不允许使用相同的名称定义新的私有字段,这与OOD中经典的封装范例是正交的。

So I am a bit frustrated. Maybe I am expecting too much from the language because it's nothing more than a build up over standard C.

所以我有点沮丧。也许我对这门语言期望太高了,因为它只不过是标准C语言的一个增强而已。

So do you use @private in your code? Why?

那么您在代码中使用@private吗?为什么?

4 个解决方案

#1


2  

I guess it's a good idea to always use @private, but I've never bothered in the past because I generally use property accessors for almost all ivar access except in init and dealloc methods. So in practice, I rarely have a problem of accessing ivars by mistake.

我想总是使用@private是个好主意,但我以前从来没有遇到过麻烦,因为除了init和dealloc方法外,我几乎对所有ivar访问都使用属性访问器。所以在实践中,我很少有错误地访问ivars的问题。

Also, if you're targeting iOS 4+, you don't need to declare ivars for properties if you use @synthesize.

此外,如果您针对的是iOS 4+,如果使用@synthesize,则不需要为属性声明ivars。


I should note that if you're writing library code that is meant to be subclassed by other developers, the use of @private would be more important.

我应该注意,如果您正在编写的库代码是由其他开发人员进行子类化的,那么使用@private将更为重要。

#2


2  

I do out of habit, but it really doesn't matter unless you're shipping a binary framework others will link agianst, which I'm pretty sure you're not doing.

我这么做是出于习惯,但这真的没关系,除非你发布了一个二进制框架,其他人会链接到agianst,我很确定你不会这么做。

All @private does is restrict the visibility of the members of the object struct (accessed like obj->_ivar, rather than [obj getter] or obj.getter). It's a nice sanity check since it will error if you try to do it outside the class -- pretty much the only place to use direct structure access is when implementing NSCoding or NSCopying and those will still work -- but it doesn't really buy you much.

所有@private都限制了对象结构成员的可见性(访问类似obj->_ivar,而不是[obj getter]或obj.getter)。这是一个很好的完整性检查,因为如果您试图在类之外执行它,它将会出错——几乎惟一使用直接结构访问的地方是在实现NSCoding或NSCopying时,这些操作仍然有效——但这并不能真正为您带来什么好处。

#3


1  

It's only really useful for Apple or folks who are shipping libraries that want to expose certain fields only to themselves in header files. We never use it because the accessor model lets you expose (or not) what you want. Since I have both header and source files what good is private really? Objective-C isn't C++ so @private has a different purpose.

它只对苹果或那些只希望在头文件中对自己公开某些字段的人有用。我们从不使用它,因为accessor模型允许您公开(或不公开)您想要的内容。既然我有头文件和源文件,那么什么才是私有的呢?Objective-C不是c++,所以@private有不同的用途。

#4


1  

In all the code I've written in Objective-C since 1989, I've never bothered to use @public, @protected, or @private.

自1989年以来,在我用Objective-C编写的所有代码中,我从未费心使用@public、@protected或@private。

#1


2  

I guess it's a good idea to always use @private, but I've never bothered in the past because I generally use property accessors for almost all ivar access except in init and dealloc methods. So in practice, I rarely have a problem of accessing ivars by mistake.

我想总是使用@private是个好主意,但我以前从来没有遇到过麻烦,因为除了init和dealloc方法外,我几乎对所有ivar访问都使用属性访问器。所以在实践中,我很少有错误地访问ivars的问题。

Also, if you're targeting iOS 4+, you don't need to declare ivars for properties if you use @synthesize.

此外,如果您针对的是iOS 4+,如果使用@synthesize,则不需要为属性声明ivars。


I should note that if you're writing library code that is meant to be subclassed by other developers, the use of @private would be more important.

我应该注意,如果您正在编写的库代码是由其他开发人员进行子类化的,那么使用@private将更为重要。

#2


2  

I do out of habit, but it really doesn't matter unless you're shipping a binary framework others will link agianst, which I'm pretty sure you're not doing.

我这么做是出于习惯,但这真的没关系,除非你发布了一个二进制框架,其他人会链接到agianst,我很确定你不会这么做。

All @private does is restrict the visibility of the members of the object struct (accessed like obj->_ivar, rather than [obj getter] or obj.getter). It's a nice sanity check since it will error if you try to do it outside the class -- pretty much the only place to use direct structure access is when implementing NSCoding or NSCopying and those will still work -- but it doesn't really buy you much.

所有@private都限制了对象结构成员的可见性(访问类似obj->_ivar,而不是[obj getter]或obj.getter)。这是一个很好的完整性检查,因为如果您试图在类之外执行它,它将会出错——几乎惟一使用直接结构访问的地方是在实现NSCoding或NSCopying时,这些操作仍然有效——但这并不能真正为您带来什么好处。

#3


1  

It's only really useful for Apple or folks who are shipping libraries that want to expose certain fields only to themselves in header files. We never use it because the accessor model lets you expose (or not) what you want. Since I have both header and source files what good is private really? Objective-C isn't C++ so @private has a different purpose.

它只对苹果或那些只希望在头文件中对自己公开某些字段的人有用。我们从不使用它,因为accessor模型允许您公开(或不公开)您想要的内容。既然我有头文件和源文件,那么什么才是私有的呢?Objective-C不是c++,所以@private有不同的用途。

#4


1  

In all the code I've written in Objective-C since 1989, I've never bothered to use @public, @protected, or @private.

自1989年以来,在我用Objective-C编写的所有代码中,我从未费心使用@public、@protected或@private。