何时使用强属性或弱属性 [英] When to use strong or weak for properties
问题描述
我有一个表视图作为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 IBOutlet
s?
推荐答案
在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
设置为strong
或weak
.
对于您面临的警告问题,您能否提供更多详细信息和代码?
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:
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屋!