IOS,ARC,属性:(读写,非原子)vs(radwrite,retain,nonatomic) [英] IOS, ARC, Property: (readwrite, nonatomic) vs (radwrite, retain, nonatomic)

查看:139
本文介绍了IOS,ARC,属性:(读写,非原子)vs(radwrite,retain,nonatomic)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经阅读了一些关于ARC的教程,还是有点困惑的属性声明。我使用以下模式写了最多的代码:

  @property(readwrite,nonatomic)Pl​​ayerData * playerData; 
@property(readwrite,nonatomic)MusicLayer * musicLayer;
@property(readwrite,nonatomic)bool isPowerUpAvailable;

现在我终于开始处理内存泄漏了XCode建议我在一些位的代码我应该在属性声明中添加了retain关键字。



使用ARC我想我不应该保留关于保留计数了。有没有一些概念,我没有得到或失踪?任何教程参考或解释将非常感谢。

解决方案

ARC将根据属性声明保留对象,应使用 strong 用于需要保留的属性, weak 用于不需要保留的属性。





编译器将始终假定属性 weak $ c> readwrite 所以没有必要以这种方式声明。

  @property(strong,nonatomic)Pl​​ayerData * playerData; 
@property(strong,nonatomic)MusicLayer * musicLayer;
//需要使用赋值,因为strong仅用于对象。
@property(assign,nonatomic)bool isPowerUpAvailable;


I am have read up some tutorials on ARC and am still left a bit confused on properties declarations. I wrote most most my code using the following pattern:

@property (readwrite, nonatomic) PlayerData* playerData;
@property (readwrite, nonatomic) MusicLayer* musicLayer;
@property (readwrite, nonatomic) bool isPowerUpAvailable;

Now that I finally started to deal with memory leaks XCode suggested me that in some bits of code I should have added the "retain" keyword in the property declaration.

Using ARC I thought I shouldn't "Bother" about retain counts anymore. Is there some concept I am not getting or missing? Any tutorial references or explanation would be greatly appreciated.

解决方案

ARC is will retain object based on the property declaration, you should use strong for properties that need to be retained and weak for properties that do not need to be retained.

weak properties are also nilled when the object is deallocated.

The compiler will always assume that properties are readwrite so there is no need to declare then this way.

@property (strong, nonatomic) PlayerData* playerData;
@property (strong, nonatomic) MusicLayer* musicLayer;
// Need use assign since strong is for objects only.
@property (assign, nonatomic) bool isPowerUpAvailable;

这篇关于IOS,ARC,属性:(读写,非原子)vs(radwrite,retain,nonatomic)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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