Objective-C 类没有属性?

发布于 2024-10-30 08:23:14 字数 158 浏览 5 评论 0原文

我正在查看一个大型项目中的一些代码,我注意到在几个类中,创建了实例变量,但没有创建相应的属性(@property)。

创建没有属性的实例变量是“错误的”吗?这不会成为内存管理问题吗?

实际上我以前从未见过这样的代码,所以我不知道此时该怎么想。

提前致谢!

I'm in the process of looking over some code in a large project, and I have noticed that in several of the classes, instance variables are created but no corresponding properties (@property) are created.

Is it "wrong" to create instance variables without properties? Doesn't this become a memory management issue?

I've actually never seen code like this before so I'm not sure what to think at this point.

Thanks in advance!

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

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

发布评论

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

评论(4

近箐 2024-11-06 08:23:14

@properties 只是简写——非常方便的简写——用于您可以自己编写的代码,没有什么魔力。

@properties 也可能是在类扩展内的实现文件中声明的,并且没有可公开访问的 API 来直接操作实例变量。

@properties are merely shorthand -- very convenient short-hand -- for code you can write yourself, no magic about it.

It may also be that the @properties are declared in the implementation file within a class extension and there is no publicly accessible API for directly manipulating the instance variables.

雨轻弹 2024-11-06 08:23:14

没有理由必须使用 Objective-C 2 风格的 setter/getter 来管理实例变量 - 只要实例变量在 dealloc 方法中释放(如果确实是分配/初始化的对象等),那么就有没什么好担心的。

请记住,在 Objective-C 之前,此类属性(以及整个 @property/@synthesize 语法)根本不存在,因此您必须创建自己的属性getters/setters(如果您认为有必要/方便的话)。

There's no reason that you have to use the Objective-C 2 style setters/getters to manage your instance variables - as long as the instance variable is released within the dealloc method (if indeed it's a alloced/inited object, etc.) then there's nothing to worry about.

Bear in mind that prior to Objective-C, such properties (and the whole @property/@synthesize syntax) simply didn't exist, so you had to create your own getters/setters if you deemed it necessary/convenient.

拥抱我好吗 2024-11-06 08:23:14

一点也不。实例变量工作正常,并且与其他任何变量都遵循相同的内存管理规则:在将其保存到实例变量之前保留它,并确保在不再需要它时释放它(通常在dealloc中) )。

这里的一些历史记录可能会有所帮助:

一开始,只有实例变量。按照惯例,“属性”仅以非正式的方式存在,以便类外部的对象访问该类公开的“公共”数据。您可以为每个方法编写自己的 -(Foo *)foo-(void)setFoo:(Foo *)f 方法。这些通常就像样板代码,在第一种情况下简单地返回 ivar,并在后者中执行正确的保留/释放/设置舞蹈。

因此 Objective-C 2.0 出现了,使我们能够使用语言语法声明属性,甚至为我们生成访问器——节省了大量时间和样板代码。

随着时间的推移,一些人开始将所有伊瓦尔视为“财产”,无论是公共的还是私人的。公共接口在 .h 文件中显示为 @properties,但您也可以在 .m 文件中为对象创建一个私有接口,声明您的“私有”@properties >,这样您就可以在类中使用访问器。这可能会也可能不会太过分,这取决于你的哲学,但我认为这与你现在看到的情况有关,裸体的伊瓦尔看起来很可疑。

他们不应该。实例变量可以在没有任何其他机制的情况下愉快地存在。只需正确保留/释放即可(在非 GC 运行时)。

当您变得更高级时,请参阅@bbum对此问题的回答:
每个 ivar 都必须是属性吗?
对于一些更多关于 KVO 和子类化属性的好处的大学思考。

Not at all. Instance variables work fine, and are subject to the same memory management rules as anything else: retain it before saving it to the instance var, and make sure you release it when you don't need it anymore (typically in the dealloc).

Some history here might be helpful:

In the beginning, there were only instance variables. "Properties" existed only in an informal way, by convention, for objects outside your class to access "public" data that the class exposed. You'd write your own -(Foo *)foo and -(void)setFoo:(Foo *)f methods for each of these. Those often were like boilerplate code, trivially returning the ivar in the first case, and doing the right retain/release/set dance in the latter.

So Objective-C 2.0 came along and gave us the ability to declare properties with the language syntax, and even generate the accessors for us-- lots of time and boilerplate code was saved.

As time went on, some people began to think about all ivars as "properties", public or private. The public ones appear in the .h file as @properties, but you can also create a private interface to your object in the .m file that declare your "private" @properties, so you can use the accessors inside your class. This might or might not be overkill, depending on your philosophy to it, but this I think has to the situation you see now, where naked ivars look suspicious.

They shouldn't. Instance variables happily exist without any of the other machinery. Just get your retain/release right (in non-GC runtimes).

As you get more advanced, see @bbum's answer to this question:
Must every ivar be a property?
for some more varsity things to think about around the benefits of properties around KVO and subclassing.

吃→可爱长大的 2024-11-06 08:23:14

实例变量的属性不是强制性的。事实上,在 Objective-C v2.0 之前,没有属性这样的东西——你必须为实例变量编写自己的访问器和修改器(如果你想在类之外访问它们)。属性可以简化内存管理,但是说实话,ivars的内存管理并没有那么难,自己搞定也不难。

Properties for instance variables aren't mandatory. In fact, prior to v2.0 of Objective-C, there was no such thing as properties -- you had to write your own accessors and mutators for instance variables (if you wanted to access them outside of the class). Properties can simplify memory management, but to be honest, memory management of ivars isn't that difficult, and it's not hard to handle yourself.

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