strong与weak是由ARC新引入的对象变量属性
xcode 4.2(ios sdk4.3和以下版本)和之前的版本使用的是retain和assign,是不支持ARC的。xcode 4.3(ios5和以上版本)之后就有了ARC,并且开始使用
assign: 用于非指针变量。用于
如:
反正记住:前面不需要加 “*” 的就用assign吧
retain:用于指针变量。就是说你定义了一个变量,然后这个变量在程序的运行过程中会被更改,并且影响到其他方法。一般是用于字符串( NSString,NSMutableString),数组(NSMutableArray,NSArray),字典对象,视图对象(UIView ),控制器对象(UIViewController)等
比如:
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 来使用。
不过好像当一个类继承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.