retain、strong、weak、assign

还搞不清楚retain、strong、weak、assign 的可以看这个

strong与weak是由ARC新引入的对象变量属性
xcode 4.2(ios sdk4.3和以下版本)和之前的版本使用的是retain和assign,是不支持ARC的。xcode 4.3(ios5和以上版本)之后就有了ARC,并且开始使用

strong与weak





assign: 用于非指针变量。用于

基础数据类型 (例如NSInteger)和C数据类型(int, float, double, char, 等),另外还有id

如:

@property (nonatomic, assign) int number;
@property (nonatomic, assign) id className;//id必须用 assign

反正记住:前面不需要加 “*” 的就用assign


retain:用于指针变量。就是说你定义了一个变量,然后这个变量在程序的运行过程中会被更改,并且影响到其他方法。一般是用于字符串( NSString,NSMutableString),数组(NSMutableArray,NSArray),字典对象,视图对象(UIView ),控制器对象(UIViewController)等
比如:

@property (nonatomic, retain ) NSString * myString;
@property (nonatomic, retain) UIView * myView;
@property (nonatomic, retain) UIViewController * myViewController;

xcode 4.2不支持ARC,所以会频繁使用retain来修饰,用完释放掉,而xcode4.3支持ARC,可以使用retian,不需要手动释放内存,系统会自动为你完成,如果你在xcode4.3上面开发,retian和strong都是一样的,没区别


strong和weak:
事实上
@property(nonatomic,strong) MyClass *myObject;就是相当于@property(nonatomic,retain) MyClass *myObject;@property(nonatomic, weak )id<RNNewsFeedCellDelegate>delegate;就是相当于@property(nonatomic,assign )id<RNNewsFeedCellDelegate>delegate; 
现在系统自动生成的属性都是用weak来修饰的,我想应该是xcode 4.2不支持ARC,所以大家都是用retain。现在xcode4.3支持ARC了,于是苹果建议程序员放弃retain,以后都用weak。
weak 就是相当于assign,同样可以在xcode4.3开发环境下放弃使用assign 使用weak 来代替


unsafe_unretained 
unsafe_unretained 就是ios5版本以下的 assign ,也就是 unsafe_unretained , weak, assign 三个都是一个样的。 因为 ios5用的是 weak ,那在ios4.3就用不了,如果你将 weak 修改为 unsafe_unretained ,那就可以用了。说到底就是iOS 5之前的系统用该属性代替 weak 来使用。


copy:这个东西估计是大部分人最不容易搞明白的东西,我也搞不明白。听别人说这个东西基本不用了,效果其实和retain没什么两样,唯一的区别就是 copy只用于NSString而不能用于NSMutableString

不过好像当一个类继承NSObject,那么这个类里面的属性需要使用copy,比如:
#import <Foundation/Foundation.h>
#import <MapKit/MKAnnotation.h>
@interface Annotation : NSObject <MKAnnotation> {
        
        CLLocationCoordinate2D coordinate;
        NSString *title;
        NSString *subtitle;
}
@property (nonatomic) CLLocationCoordinate2D coordinate;
@property (nonatomic, copy) NSString *title;
@property (nonatomic, copy) NSString *subtitle;
@end
反正以后就这么用就是了 



反正就记住一点:xcode4.2用retain和assign ;xcode4.3或以上版本用strong与weak 。以前用xcode4.2开发程序的程序员会习惯用retain ,所以代码都是retian的,新手如果从xcode4.3学起的话就用strong与weak  吧

strong

Properties of this type will be retained by the runtime. These can only be instancesof classes. In other words, you cannot retain a value into a property of type strongif the value is a primitive. You can retain objects, but not primitives.

copy
The same as strong, but when you assign to properties of this type, the runtimewill make a copy of the object on the right side of the assignment. The object onthe righthand side of the assignment must conform to the NSCopying or NSMutableCopying protocol.

assign
Objects or primitive values that are set as the value of a property of type assign willnot be copied or retained by that property. For primitive properties, this qualifierwill create a memory address where you can put the primitive data. For objects,properties of this type will simply point to the object on the righthand side of theequation.

unsafe_unretained
The same as the assign qualifier.

weak
The same as the assign qualifier with one big difference. In the case of objects,when the object that is assigned to a property of this type is released from memory,the runtime will automatically set the value of this property to nil

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值