何时使用强属性或弱属性 [英] When to use strong or weak for properties

查看:112
本文介绍了何时使用强属性或弱属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表视图作为IBOutlet,默认情况下,XCode将其属性设置为strong而不是weak.有时我会收到收到内存警告"消息.因此,我尝试将许多属性从strong更改为weak,但是它似乎并没有影响过程,并且一切运行正常.我应该将网点设置为弱点,还是我错了?

I have a table view as an IBOutlet, and by default XCode sets its property to be strong rather than weak. Sometimes I get a "recieved memory warning" message. So I tried to change many properties from strong to weak, but it doesn't seem to affect the process and things work smoothly. Should I set the outlets to weak, or am I wrong?

最重要的是,应该在viewDidUnload方法中将 ALL 属性设置为nil还是仅设置IBOutlet s?

And most importantly, should I set ALL properties to nil in the viewDidUnload method, or only the IBOutlets?

推荐答案

viewDidUnload中,应仅将Strong属性设置为nil. Weak如果释放了目标对象,则属性会自动设置为Nil.

You should set only Strong properties to nil in viewDidUnload. Weak Properties are automatically set to Nil if the destination object is deallocated.

IBOutlet设置为strongweak.

对于您面临的警告问题,您能否提供更多详细信息和代码?

For the warning issue you are facing can you provide more details and code?

除了Josh提供的链接之外,还有很多与此主题相关的SO帖子,如下:

Apart from link provided by Josh, there are a lot of posts on SO related to this topic, some are below:

对于IBOutlet和其他产品弱或强

Objective-C声明的@property属性(非原子,复制,强,弱)

可以在这里找到很好的详细说明:

Good detailed explanation can be found here:

http://www.raywenderlich.com /5677/beginning-arc-in-ios-5-part-1

有关此主题的Apple文档可在以下位置找到: http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/ObjectiveC/Chapters/ocProperties.html#//apple_ref/doc/uid/TP30001163-CH17-SW1

Apple docs on this topic can be found here: http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/ObjectiveC/Chapters/ocProperties.html#//apple_ref/doc/uid/TP30001163-CH17-SW1

这篇关于何时使用强属性或弱属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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