将 IBOutlet 放在实例变量声明中还是 @property 声明中?
Objective-C for iPad,你把IBOutlet放在哪里?在实例变量声明或@property声明中?有什么区别吗?
Objective-C for iPad, Where do you put IBOutlet? In instance variable declaration or @property declaration? Is there a difference at all?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
IBOutlet 可以是 ivars 上的标记或属性声明。
有一点细微的差别。 IBOutlet 属性通过访问方法,而 IBOutlet ivars 是直接 ivar 访问。
主要区别在于,如果保留 IBOutlet 属性,则必须在
-dealloc
中释放它,而通常不需要对 IBOutlet ivar 执行任何操作。 IBOutlet 属性的优点是您可以获得属性的所有有用功能。IBOutlet can be a marker on ivars or a property declaration.
There is a slight difference. IBOutlet properties go through access methods whereas IBOutlet ivars are direct ivar access.
The major difference is that if the IBOutlet property is retained, you'll have to release it in
-dealloc
whereas you typically need not do anything with an IBOutlet ivar. The upside of IBOutlet property is that you get all the useful features of properties.两者都是有效的,即使通常建议将其放在房产上。
与属性的区别在于它可以从外部获取,并且使用 getter/setter 方法。
这还允许通常为 IBOutlet 设置的属性限定符,例如非原子和保留。
Both are valid, even if it's usually recommended to put it on a property.
The difference with a property is that it's available from the outside, and that getter/setter methods are used.
That also allows property qualifiers, like non-atomic and retain, usually set for the IBOutlets.
mmalc(绝对是一个有信誉的来源)表示,当前的最佳实践是将其放在 @property 声明中。他在他对此的回答中提供了详细信息(以及一些警告)问题
mmalc (who is definitely a reputable source) says that the current best-practice is putting it on the @property declaration. He gives details (along with some cavets) in his answer to this quiestion
两者都有效我建议您使用@property
Both are valid I suggest you to use @property
我两者都做了,并将其综合在 .m 文件中。我并不是 100% 认为它们都是必不可少的,但这并没有坏处。
I do both, and synthesise it in the .m file. I'm not 100% that they're both essential, but it doesn't hurt.