公共物品和财产的使用

发布于 2024-11-17 23:49:29 字数 491 浏览 3 评论 0原文

我有点困惑;如果在 .h 文件中声明一个对象,它会自动被视为“公共”,对吧?但是,我们在 .h 文件中使用 @property 来编辑它们?这是我不明白的地方:我们对私有对象使用 getter/setter,那么为什么我们对 .h 文件中声明的对象使用 @property 并因此被视为“公共”?

第二件事,我发现了这个例子:我不明白为什么我们在这段代码中使用 @synthesize 作为 primaryKeyhttp://staging.icodeblog.com/wp-content/uploads/2008/08/9-todom1.png 为什么我们不使用 @property 作为 database 对象?

I'm a bit confused; if an object is declared in the .h file it is considered automatically as "public" right? We use a @property in the .h file, however, to edit them? This is where I don't understand: we use the getter/setter for private objects, so why do we use the @property for objects declared in the .h file and thus considered as "public"?

Second thing, I found this example: I don't understand why we use a @synthesize for primaryKey in this code: http://staging.icodeblog.com/wp-content/uploads/2008/08/9-todom1.png
and why we don't use a @property for the database object?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

不爱素颜 2024-11-24 23:49:29

如果在 .h 文件中声明一个对象 (ivar),则它是公共的,这是不正确的。仅当提供 getter/setter 方法时才有效,否则不提供。

事实上,@property/@synthesize 指令是用于声明和定义默认 getter/setter 方法的工具。因此,您不必自己编写它们,而只需使用指令即可。

还值得注意的是,在声明属性时,您可以使用点符号来引用对象的属性。而且,由于 retain/assign/copy 说明符,它们澄清了很多,即如何为该属性管理内存。 (当然,@synthesize 会为您正确执行此操作)。

事实上,关于您的示例,ivar 是否与属性关联是一个设计选择。可能,您只需重新考虑 .h 文件中声明的 ivars 默认情况下是公共的假设,它就会变得更清晰。换句话说:primaryKey 是公共的,database 不是。

可以在此处找到一个非常好的教程,但也不要忘记苹果文档

编辑:

关于评论部分的问题:

没有必要每个 ivar 都有一个属性,也没有必要有 getter/setter 才能在该类实现内部使用。

@interface SomeClass : NSObject {
    AnotherClass* _anotherClassObj;
    AThirdClass* _aThirdClassObj;
}
@property (nonatomic, retain) AnotherClass* anotherClassObj;
@end

所以,这里有两个 ivars;只有一个拥有 @property 声明。在您的 .m 文件中,您可能有,例如

@implementation SomeClass;
@synthesize anotherClassObj = _anotherClassObj;

- (void)initWithClasses:(AnotherClass*)obj1 and:(AThirdClass*)obj2 {
   .....
   self.anotherClassObj = obj1;
   _aThirdClassObj = obj2;
   ...
}

....
@end

在此代码中:

  1. @synthesize 将为 anotherClassObj 提供 getter/setter 实现,因此您可以使用语法:self.anotherClassObj = obj1;该语法可以在类实现内部和外部同等使用;

  2. 当您没有 getter/setter(自动生成或自定义)时,您可以使用语法 _aThirdClassObj = obj2; 直接分配给 ivar,并具有简单指针复制的语义;无论如何,_aThirdClassObj 无法从该类外部访问;

  3. 此外,@property ... anotherClassObj 尽管如此,您仍然可以直接在 .m 文件中引用 _anotherClassObj,例如 _anotherClassObj = xxx >,绕过 getter/setter,如果你需要的话。

您应该清楚的一件事是 getter/setter 不仅仅是使 ivar“公开”的一种方式。它们在管理保留计数方面也发挥着重要作用(取决于您在属性声明中的保留/分配/复制中选择的说明符)。因此,在上面的 self.anotherClassObj = obj1; 中,obj1 被分配给 _anotherClassObj 并且它也被保留(并且如果 _anotherClassObj< /code> 之前指向一个对象,该对象将被发送一个 release)。原始 ivar 分配不提供这种便利。

在我看来,对于决定我是否使用某个属性,属性的保留计数管理功能比可见性更重要。

It is not correct that if an object (ivar) is declared in a .h file, then it is public. It is only if getter/setter methods are provided, otherwise it is not.

Indeed, the @property/@synthesize directives are facilities meant to declare and define default getter/setter methods. So, instead of writing them yourself, you just use the directives.

It is also worth noting that declaring properties you get the possibility of using the dot notation to refer properties of your objects. And also that they clarify a lot, thanks to the retain/assign/copy specifiers, how memory is meant to be managed for that properties. (And, of course, @synthesize will just do that correctly for you).

About your sample, in fact, whether an ivar is associated to a property or not is a design choice. Possibly, you just reconsider the assumption that ivars declared in .h files are public by defaults, and it will become clearer. In other words: primaryKey is public, database is not.

A very nice tutorial can be found here but also do not forget Apple docs.

EDIT:

about your question from the comment section:

it is not necessary that every ivar has a property, nor that it has getter/setter in order to be used inside of that class implementation.

@interface SomeClass : NSObject {
    AnotherClass* _anotherClassObj;
    AThirdClass* _aThirdClassObj;
}
@property (nonatomic, retain) AnotherClass* anotherClassObj;
@end

So, here you have two ivars; only one has got a @property declaration. In your .m file you may have, e.g.

@implementation SomeClass;
@synthesize anotherClassObj = _anotherClassObj;

- (void)initWithClasses:(AnotherClass*)obj1 and:(AThirdClass*)obj2 {
   .....
   self.anotherClassObj = obj1;
   _aThirdClassObj = obj2;
   ...
}

....
@end

In this code:

  1. @synthesize will provide implementation for getter/setter for anotherClassObj so you can use syntax: self.anotherClassObj = obj1; that syntax can be used equally from inside and outside the class implementation;

  2. when you have no getter/setter (either auto-generated or custom) you can assign directly to an ivar by using the syntax _aThirdClassObj = obj2;, with the semantics of simple pointer copy; anyway, _aThirdClassObj will not accessible from outside that class;

  3. furthermore, @property ... anotherClassObj notwithstanding, you can still refer _anotherClassObj directly in your .m file, like in _anotherClassObj = xxx, bypassing getter/setter, if you ever need it.

One thing you should have clear is that getter/setter are not only a way to make an ivar "public". They also play an important role in managing the retain count (depending on which specifier you choose among retain/assign/copy in the property declaration). So, in self.anotherClassObj = obj1; above, obj1 is assigned to _anotherClassObj and it is also retained (and if _anotherClassObj was previously pointing to an object, that object will be sent a release). Raw ivar assignment does not provide that kind of facility.

In my opinion, the retain count management feature of properties is far more important than visibility for deciding whether I use a property or not.

丘比特射中我 2024-11-24 23:49:29

并非标头中的所有内容都是公开的,默认情况下 ivars({ } 中的项目)是 @protected。 @property的目的是数据封装。 @synthesize 或 @dynamic 用于声明您想要实现属性的方式,并且其中之一对于防止崩溃和警告是必要的。

资源:
定义类     @protected、@package、@private、@public 引用
声明的属性 @属性参考

Not everything in the header is public, by default ivars (items in the { }) are @protected. The purpose of the @property is data encapsulation. @synthesize or @dynamic is used for declaring the way you want to implement your property and one or the other is necessary to prevent crashes and warnings.

Resources:
Defining Classes        @protected, @package, @private, @public reference
Declared Properties   @property reference

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文