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?
我有一个表视图作为IBOutlet,默认情况下,XCode将其属性设置为强而不是弱。有时我收到“收到内存警告”的消息。所以我试图将许多属性从强变为弱,但它似乎不会影响流程,事情也很顺利。我应该把插座设置为弱电,还是我错了?
And most importantly, should I set ALL properties to nil
in the viewDidUnload
method, or only the IBOutlet
s?
最重要的是,我应该在viewDidUnload方法中将ALL属性设置为nil,还是仅将IBOutlets设置为nil?
2 个解决方案
#1
1
You should set only Strong
properties to nil
in viewDidUnload
. Weak
Properties are automatically set to Nil if the destination object is deallocated.
您应该在viewDidUnload中仅将强属性设置为nil。如果目标对象已取消分配,则弱属性将自动设置为Nil。
IBOutlet
can be set to strong
or weak
based on the requirement.
IBOutlet可根据要求设置为强或弱。
For the warning issue you are facing can you provide more details and code?
对于您面临的警告问题,您可以提供更多详细信息和代码吗?
Apart from link provided by Josh, there are a lot of posts on SO related to this topic, some are below:
除了Josh提供的链接之外,还有很多与此主题相关的帖子,其中一些如下:
weak or strong for IBOutlet and other
IBOutlet和其他弱者或弱者
Objective-C declared @property attributes (nonatomic, copy, strong, weak)
Objective-C声明@property属性(非原子,复制,强,弱)
Good detailed explanation can be found here:
可以在这里找到详细的解释:
http://www.raywenderlich.com/5677/beginning-arc-in-ios-5-part-1
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
关于这个主题的Apple文档可以在这里找到:http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/ObjectiveC/Chapters/ocProperties.html#//apple_ref/doc/uid/TP30001163-CH17 -SW1
#2
0
"When a parent has a reference to a child object, you should use a strong reference. When a child has a reference to its parent object, you should use a weak reference"
“当父对象具有对子对象的引用时,应使用强引用。当子对象引用其父对象时,应使用弱引用”
In general weak references are used when you deal with memory cycles. if you use strong you need to set nil in viewDidUnload since if you don't do it, in memory low conditions, you could cause unexpected leaks. You don't release them in dealloc because ARC will do it for you. weak instead doesn't need that treatment since, when the target object is destroyed, those values are set as nil automatically. No dangling pointers anymore.
通常,在处理内存周期时会使用弱引用。如果使用strong,则需要在viewDidUnload中设置nil,因为如果不这样做,在内存不足的情况下,可能会导致意外泄漏。你不会在dealloc中释放它们,因为ARC会为你做这件事。因为当目标对象被破坏时,这些值被自动设置为nil,因此不需要该处理。没有悬空指针了。
#1
1
You should set only Strong
properties to nil
in viewDidUnload
. Weak
Properties are automatically set to Nil if the destination object is deallocated.
您应该在viewDidUnload中仅将强属性设置为nil。如果目标对象已取消分配,则弱属性将自动设置为Nil。
IBOutlet
can be set to strong
or weak
based on the requirement.
IBOutlet可根据要求设置为强或弱。
For the warning issue you are facing can you provide more details and code?
对于您面临的警告问题,您可以提供更多详细信息和代码吗?
Apart from link provided by Josh, there are a lot of posts on SO related to this topic, some are below:
除了Josh提供的链接之外,还有很多与此主题相关的帖子,其中一些如下:
weak or strong for IBOutlet and other
IBOutlet和其他弱者或弱者
Objective-C declared @property attributes (nonatomic, copy, strong, weak)
Objective-C声明@property属性(非原子,复制,强,弱)
Good detailed explanation can be found here:
可以在这里找到详细的解释:
http://www.raywenderlich.com/5677/beginning-arc-in-ios-5-part-1
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
关于这个主题的Apple文档可以在这里找到:http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/ObjectiveC/Chapters/ocProperties.html#//apple_ref/doc/uid/TP30001163-CH17 -SW1
#2
0
"When a parent has a reference to a child object, you should use a strong reference. When a child has a reference to its parent object, you should use a weak reference"
“当父对象具有对子对象的引用时,应使用强引用。当子对象引用其父对象时,应使用弱引用”
In general weak references are used when you deal with memory cycles. if you use strong you need to set nil in viewDidUnload since if you don't do it, in memory low conditions, you could cause unexpected leaks. You don't release them in dealloc because ARC will do it for you. weak instead doesn't need that treatment since, when the target object is destroyed, those values are set as nil automatically. No dangling pointers anymore.
通常,在处理内存周期时会使用弱引用。如果使用strong,则需要在viewDidUnload中设置nil,因为如果不这样做,在内存不足的情况下,可能会导致意外泄漏。你不会在dealloc中释放它们,因为ARC会为你做这件事。因为当目标对象被破坏时,这些值被自动设置为nil,因此不需要该处理。没有悬空指针了。