变量属性strong,weak,retain,assign的区别
2014-06-21 22:36
295 查看
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
4000
<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
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
4000
<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
相关文章推荐
- object-c学习:@property 属性中 assign,nonatomic,retain,strong,weak的区别
- 属性的特性assign,retain,copy,strong,weak,nonatomic,atomic,readonly,readwrite的区别与介绍
- 【整理】Object-C中的属性(Property)的Setter:assign,copy,retain,weak,strong之间的区别和联系
- property中的属性strong 、weak、copy 、assign 、retain 、unsafe_unretained 与autoreleasing区别和作用详解
- object-c学习:@property 属性中 assign,nonatomic,retain,strong,weak的区别
- iOS中 property中的属性strong 、weak、copy 、assign 、retain 、unsafe_unretained 与autoreleasing区别和作用详解
- property中的strong 、weak、copy 、assign 、retain 、unsafe_unretained 与autoreleasing区别和作用详解
- iOS_assign,retain,weak,strong的引用计数区别(附代码示例)
- ios中assign、copy 、retain、strong、weak的区别
- retain和strong、assign和weak的区别
- assign、copy、retain、weak、strong的区别与联系
- strong,weak, retain, assign的区别
- copy,assign,strong,retain,weak,readonly,nonatomic的区别
- strong assign weak retain copy 区别
- iOS strong .copy .retain .weak .assign的区别,以及各自的用法
- copy,assign,strong,retain,weak,readonly,nonatomic的区别
- copy,assign,strong,retain,weak,readonly,nonatomic的区别
- property中的strong 、weak、copy 、assign 、retain 、unsafe_unretained 与autoreleasing区别和作用详解
- IOS中属性的关键修饰词:strong weak assign copy retain等。。
- copy,assign,strong,retain,weak,readonly,nonatomic的区别