简体   繁体   English

在启用ARC的情况下将强引用对象设置为nil?

[英]Setting strong references objects to nil with ARC enabled?

I am developing an iPhone app with ARC option enabled. 我正在开发启用了ARC选项的iPhone应用程序。 i am creating IBOutlets in .h files which are connected from file owners icon to .xib elements.for eg. 我正在.h文件中创建IBOutlets,这些文件从文件所有者图标连接到.xib elements.for。

IBOutlet UIButton *bt;
@property(nonatomic,retain)IBOutlet UIButton *bt;

in .m file, i am doing 在.m文件中,我正在做

@synthesize bt;

Is there a need to explicitly set bt to nil in viewDidUnload method? 是否需要在viewDidUnload方法中将bt显式设置为nil? ie self.bt = nil; self.bt = nil; in viewDidUnload ? viewDidUnload

Also, do I need to write dealloc method with ARC option enabled? 另外,是否需要在启用ARC选项的情况下编写dealloc方法? When should I make IBOutlets elements as strong and weak references with ARC enabled? 在启用ARC的情况下,何时应将IBOutlets元素作为强引用和弱引用?

There's a difference between the need to put your outlets to nil in viewDidUnload and dealloc viewDidUnloaddealloc中将网点设置为零的需求之间是有区别的

Having ARC means you don't need to write that in your dealloc method (it gets done automatically), however the viewDidUnload method serves another purpose, and it is to free memory that the application isn't using when a memory warning occurs. 拥有ARC意味着您不需要在dealloc方法中编写它(它会自动完成),但是viewDidUnload方法还有另一个用途,它是在出现内存警告时释放应用程序未使用的内存。 dealloc is still needed in some cases, for example, when your class is registered for a notification, or when your class is someone else's delegate and you don't want some glitchy callback giving you a bad access 在某些情况下,例如,当您的课程注册为通知时,或者当您的课程是其他人的委托并且您不希望出现一些故障回调时,仍然需要使用dealloc

When you get a memory warning, all the UIViewControllers not being displayed will unload their view and call that method to free up memory. 当您收到内存警告时,所有未显示的UIViewControllers将卸载其视图并调用该方法以释放内存。 If you are still retaining outlets (like buttons, tables and etc) they will not get released, thus, killing the purpose of the viewDidUnload method. 如果您仍然保留插座(如按钮,表格等),它们将不会被释放,因此会破坏viewDidUnload方法的用途。

When using ARC there is no need to use modifiers like retain or copy , for example. 例如,在使用ARC时,无需使用诸如retaincopy类的修饰符。 That kind of memory managment is done automatically using the strong and weak modifiers. 使用strong修饰符和weak修饰符会自动完成这种内存管理。

You also don't have to worry about writing dealloc methods. 您也不必担心编写dealloc方法。

strong is kind of the equivalent of retain , so you should mark your outlets with it strong一种等价 retain ,所以你应该用它标记您的店铺

@property(nonatomic, strong) IBOutlet UIButton *bt;

That's the way interface builder creates them by default. 这就是界面生成器默认创建它们的方式。

I won't go into detail about their semantic differences, but you should really have a look at Apple's guide on transitioning to ARC if you want to know what's going on and read about the specifics of strong and weak modifiers. 我不会详细介绍它们的语义差异,但是如果您想了解发生了什么并了解strong修饰符和weak修饰符的细节,则应该真正查看Apple过渡到ARC的指南

EDIT: Sorry, interface builder creates outlets with weak by default. 编辑:对不起,默认情况下,界面生成器会创建带有weak的插座。

EDIT 2: strong and retain are indeed 100% identical. 编辑2: strongretain确实是100%相同。 (thanks to @Adam) (感谢@Adam)

EDIT 3: You set your pointers to nil to avoid getting any message sent to deallocated instance or BAD_ACCESS_EXCEPTION errors. 编辑3:您将指针设置为nil以避免将任何message sent to deallocated instanceBAD_ACCESS_EXCEPTION错误。

If you are actually using ARC, you should make your outlets (nonatomic, weak) instead of (nonatomic, strong) . 如果您实际使用的是ARC,则应将出口(nonatomic, weak)而不是(nonatomic, strong)做成。 By using the weak zeroing pointers, what the compiler does is automatically set your outlets to nil when nothing else references them. 通过使用weak归零指针,编译器将自动执行将出口设置为nil ,而没有其他引用它们的情况。

So, summing up, if you don't use weak properties, you should set your pointers to nil. 因此,总而言之,如果您使用weak属性, 则应将指针设置为nil。

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

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