简体   繁体   English

ARC下-viewDidUnload中的弱属性和强属性

[英]Weak and strong properties in -viewDidUnload under ARC

I am new to iphone development . 我是iPhone开发的新手。 I am using ARC for my project. 我在项目中使用ARC。 As far as I understood using ARC we don't have to release any object manually. 据我了解使用ARC,我们不必手动释放任何对象。 But , I have observed in some places , people explicitly set their object to nil in the ViewDidUnload even after using ARC. 但是,我观察到在某些地方,即使在使用ARC之后,人们也会在ViewDidUnload中将对象明确设置为nil。

For example, in .h file I have something like this: 例如,在.h文件中,我有类似以下内容:

@property (unsafe_unretained, nonatomic) IBOutlet MKMapView *mapViewOutlet;
@property (unsafe_unretained, nonatomic) IBOutlet UIToolbar *toolBar;
@property (strong,nonatomic) NSMutableArray *dataArray;

And .m as follows: 和.m如下:

- (void)viewDidUnload
{
     [self setMapViewOutlet:nil];
     [self setToolBar:nil];
     [super viewDidUnload];
     self.dataArray=nil;
}

My question is, is it really necessary to explicitly specify nil in the ViewDidUnload even under ARC? 我的问题是,即使在ARC下,是否真的有必要在ViewDidUnload中显式指定nil?

The whole point of the viewDidUnload method is to release data that you don't really need, in order to free memory. viewDidUnload方法的全部目的是释放不需要的数据,以释放内存。 Read the documentation : 阅读文档

When a low-memory condition occurs and the current view controller's views are not needed, the system may opt to remove those views from memory. 当发生内存不足的情况并且不需要当前视图控制器的视图时,系统可能会选择从内存中删除这些视图。 This method is called after the view controller's view has been released and is your chance to perform any final cleanup. 释放视图控制器的视图之后,将调用此方法,这是您执行任何最终清理的机会。 If your view controller stores separate references to the view or its subviews, you should use this method to release those references. 如果视图控制器存储对视图或其子视图的单独引用,则应使用此方法释放这些引用。 You can also use this method to remove references to any objects that you created to support the view but that are no longer needed now that the view is gone. 您也可以使用此方法删除对您创建的支持视图的所有对象的引用,但是由于视图已消失,因此不再需要这些对象。 You should not use this method to release user data or any other information that cannot be easily recreated. 您不应使用此方法来释放用户数据或任何其他不易重新创建的信息。

So you're setting the properties to nil in order to release the objects now and help the system to free up some memory. 所以你设置属性,以nil 现在为了释放对象和帮助系统释放一些内存。 But of course this depends on the property type – strong properties are “yours” and only you can decide whether to release them now (by setting to nil ) or not. 但这当然取决于属性类型-强属性是“您的”,只有您可以决定是否立即释放它们(通过设置为nil )。 Weak properties could already be nil , for example if they pointed to some views that got released with the main view. 弱属性可能已经为nil ,例如,如果它们指向与主视图一起释放的某些视图,则该属性为nil And unsafe_unretained properties are a special beast. unsafe_unretained属性是一种特殊的野兽。 The object they point to might already been released, but that does not mean they were set to nil automatically. 它们指向的对象可能已经释放,但这并不意味着它们会自动设置为nil So you should either use one of the “safer” property types (strong/weak), or set the unsafe properties to nil here, to make sure you won't use the released object later. 因此,您应该使用一种“较安全”的属性类型(强/弱),或者在此处将不安全的属性设置为nil ,以确保以后不再使用已发布的对象。 There are no hard rules in this case, you have to think about the situation and what it means for the various properties. 在这种情况下,没有硬性规定,您必须考虑情况及其对各种属性的含义。

By the way, viewDidUnload is getting deprecated in iOS 6, where no views are being released under low-memory conditions anymore. 顺便说一句, viewDidUnload在iOS 6中已被弃用,在低内存条件下不再释放任何视图。 You still receive the didReceiveMemoryWarning callback, so that you can release some resources there if you want to. 您仍然会收到didReceiveMemoryWarning回调,因此,您可以根据需要释放一些资源。 Again, I suggest that you read the documentation and run a few tests to see what happens and decide what you should do. 再次,我建议您阅读文档并进行一些测试,以了解会发生什么并决定应该做什么。

ARC will only release properties which do not hold a strong reference to an object. ARC将仅释放不持有对对象的强引用的属性。 In your case, these are all strong references, so they will be kept unless they are explicitly set to nil. 在您的情况下,这些都是强引用,因此除非明确将它们设置为nil,否则将保留它们。

The viewDidUnload method does not mean that your UIViewController is removed from memory, it simply means that its views are removed from memory ( iOS Developer - ViewController lifecycle ). viewDidUnload方法并不意味着您的UIViewController已从内存中删除,而只是意味着其视图已从内存中删除( iOS开发人员-ViewController lifecycle )。

In this case, your UIViewController remains in memory, and therefore its properties as well, unless they are explicitly set to nil. 在这种情况下,除非显式设置为nil,否则UIViewController仍保留在内存中,因此也保留在其属性中。

使用unsafe_unretained时,应将其分配给nil,因为它不会隐式分配给nil,在弱引用的情况下,它将隐式分配给nil,因此为了避免任何悬空引用,您需要将其分配给nil如果是unsafe_unretained。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM