对于Interface Builder,是否需要@ property? [英] Are @property's necessary for Interface Builder?

查看:120
本文介绍了对于Interface Builder,是否需要@ property?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的 UIViewController 子类中,我有3 UIView 属性为 IBOutlet 。我在我的代码中根本不使用这些属性。视图控制器创建后立即被实例化,当视图控制器被释放时它们被释放。

In my UIViewController subclass, I have 3 UIView's with each a @property as an IBOutlet. I do not use these properties at all in my code. The views get instantiated as soon as the view controller is created and they are deallocated when the view controller is deallocated.

我在想;我不能只删除 @property 的?我做了,我仍然可以连接我的实例变量( IBOutlet )在Interface Builder。

I was thinking; can't I just remove the @property's? I did, and I could still connect my instance variables (with IBOutlet) in Interface Builder.

现在是; 是否有任何与Interface Builder结合使用的属性,或者可以放弃它们吗?是否需要一些内存管理或什么?

So my question now is; is there any use for properties in combination with Interface Builder, or is it OK to leave them out? Is it required for some memory management or something? Or are they really just for use in your own code?

如果我把它们删除,我还需要 release 他们在 dealloc

And if I do leave them out, do I still need to release them in dealloc?

推荐答案

必要的,但他们强烈鼓励简单的事实,他们澄清内存管理。

They're not necessary, but they're strongly encouraged for the simple fact that they clarify memory management.

例如,如果您通过ivar声明了一个出口,那么它的保留策略是什么?是否保留?它是自动释放吗?你是店主吗?别人也有它吗?有很多歧义(特别是那些被诅咒的顶级对象,在Mac上的行为不同于iPhone)。

For example, if you declare an outlet via the ivar, then what is its retain policy? Is it retained? Is it autoreleased? Are you the owner? Does someone else also own it? There is a lot of ambiguity there (especially with those accursed top level objects, which behave differently on the Mac than on the iPhone).

另一方面,如果你通过属性声明出口,没有歧义,因为内存管理策略直接在声明中声明。除了存在属性,nib unarchiving将看到设置器和使用,从而确保没有什么奇怪的事情转移对象所有权等。

On the other hand, if you declare the outlet via a property, there is no ambiguity, because the memory management policy is directly stated in the declaration. In addition with the presence of the property, the nib unarchiving will see the setter and use that, thereby ensuring that nothing strange is going on with transferring object ownership, etc.

简而言之,您可以在不使用 @property 的情况下声明插件(在10.5中介绍它们之前我们必须做的),但没有很好的理由 使用他们。他们真的让代码更清楚,究竟是怎么回事。

In a nutshell, you can declare outlets without using @property (which we all had to do before they were introduced in 10.5), but there's no good reason to not use them. They really make the code a lot clearer as to what exactly is going on.

有关nib对象内存管理荒谬的详细信息,在文档中查看此页

For more info on the absurdity of nib object memory management, check out this page in the documentation.

这篇关于对于Interface Builder,是否需要@ property?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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