1、简介: 

property是Objective-C的关键词,与@synthesize配对使用,用来让编译好器自动生成与数据成员同名的方法声明。@synthesize则是用来生成对应声明方法的实现。

 

1.1 property的语法格式:

@property (参数1,参数2)类型名字;

这里的参数,主要有以下三种:

setter/getter方法(assign/retain/copy)

读写属性(readwrite/readonly)

atomicity(nonatomic)

1.2 三种方式的使用

assign/retain/copy  代表赋值的方式。

 

readonly关键字代表setter不会被生成, 所以它不可以和 copy/retain/assign组合使用。

atomicity的默认值是atomic,读取函数为原子操作。

1.2.1 copy/reain/assign 在其中选择一个来确定属性的setter如何处理这个属性。NSObject对象采用这个中方式。

1.2.2 一些特别的Object比如NSSstring使用copy。

1.2.3 assign关键字代表setter直接赋值,而不是复制或者保留它。适用于基本数据类型,比如NSInteger和CGFloat,或者你并不直接拥有的类型,比如delegates。

2、如何使用property

1.1  没有property和有property的对比

在头文件定义 obj。在.m文件中使用

 



[cpp]  ​​view plain​​ ​​copy​​



  1. #import <UIKit/UIKit.h>  
  2.   
  3. @interface ViewController : UIViewController  
  4. {  
  5.     NSObject *obj;  
  6. }  
  7. @end  



[cpp]  ​​view plain​​ ​​copy​​



  1. - (void)viewDidLoad  
  2. {  
  3.     [super viewDidLoad];      
  4.     self.obj = nil;、  
  5. }  


提示不可用。

Objective-C语法property详解_#import


加上property

 

 


[cpp]  ​​view plain​​ ​​copy​​


  1. #import <UIKit/UIKit.h>  
  2.   
  3. @interface ViewController : UIViewController  
  4. {  
  5.     NSObject *obj;  
  6. }  
  7. @property (nonatomic,retain) NSObject *obj;  
  8. @end  


编译能通过,运行,崩溃,提示错误 reason: '-[ViewController setObj:]: unrecognized selector sent to instance 0x6b6c480

 

那就是我们没事实现setter方法。

用@synthesize关键字实现getter 和setter。

在.m文件中

 


[cpp]  ​​view plain​​ ​​copy​​


  1. @implementation ViewController  
  2. @synthesize obj;  
  3. - (void)viewDidLoad  
  4. {  
  5.     [super viewDidLoad];  
  6.     self.obj = nil;  
  7. }  


运行,程序运行正常。说明setter 起作用了。

 

3、@property和@synthesize关键字 生成的代码

到底@property和@synthesize关键字生成了什么代码呢?我们自己实现getter 和setter也可以替代这些关键字。

把这两个关键字对应的代码注释掉

.h

 


[cpp]  ​​view plain​​ ​​copy​​


  1. #import <UIKit/UIKit.h>  
  2.   
  3. @interface ViewController : UIViewController  
  4. {  
  5.     NSObject *obj;  
  6. }  
  7. //@property (nonatomic,retain) NSObject *obj;  
  8. -(NSObject*)obj;  
  9. -(void)setObj:(NSObject*)newObj;  
  10. @end  


 

.m

 


[cpp]  ​​view plain​​ ​​copy​​


  1. @implementation ViewController  
  2. //@synthesize obj;  
  3. - (void)viewDidLoad  
  4. {  
  5.     [super viewDidLoad];  
  6.     self.obj = nil;  
  7. }  
  8.   
  9. -(NSObject*)obj{  
  10. return obj;  
  11. }  
  12. -(void)setObj:(NSObject*)newObj{  
  13. if(obj != newObj){  
  14.         [obj release];  
  15.         obj = [newObj retain];  
  16.     }  
  17. }  


再运行,也能正常启动。说明自己写的getter 和setter替代了property。

 

4、使用三种参数的对比

 

@property (nonatomic,retain)NSObject *obj;

@property (nonatomic,retain,readwrite) NSObject *obj;

readwrite是默认行为,所以这两行代码等价

 

 

@property (retain) NSObject *obj;

@property (atomic,retain) NSObject *obj;

atomic是默认行为,所以这两行代码是等价的。

 

 

@property(atomic,assign)int number;        

@property(atomic) int

@property int number;  

对int 来说,atomic assign都是默认行为,所以这三行是等价的。

 

@property  NSObject *obj;这样写行吗?不行的,报警告

Objective-C语法property详解_#import_02

只有int 等基础数据类型能这么写。对象必须加上赋值的类型。

 

@property  (retain) NSObject *obj;这样就没问题了。何时使用assign、何时使用retain、copy后面再讲。

5、retain和copy实验。

使用copy。

.h文件

 


[cpp]  ​​view plain​​ ​​copy​​


  1. #import <UIKit/UIKit.h>  
  2.   
  3. @interface ViewController : UIViewController  
  4. {  
  5.     NSString *string;  
  6. }  
  7. @property  (nonatomic, copy) NSString *string;  
  8. @end  


 

 

.m文件

 


[cpp]  ​​view plain​​ ​​copy​​


  1. @synthesize string;  
  2. - (void)viewDidLoad  
  3. {  
  4.     [super viewDidLoad];  
  5.       
  6. "abcd"];  
  7. "str_Point:%p  %@  retainCount:%d", str, str, [str retainCount]);  
  8.     self.string = str;  
  9. "string_Point:%p  %@  retainCount:%d", string, string, [string retainCount]);  
  10. }  


打印结果

 

 

 

2012-07-19 20:41:44.853 TestProject1[1213:f803] str_Point:0x6a8e0b0  abcd  retainCount:1

2012-07-19 20:41:44.854 TestProject1[1213:f803] string_Point:0x6a8e0b0  abcd  retainCount:2

内存地址是一样的,不是想其他文字所写的那样,拷贝了一份内存,这里用copy也是浅拷贝。retain也+1

 

使用retain

 


[cpp]  ​​view plain​​ ​​copy​​


  1. #import <UIKit/UIKit.h>  
  2.   
  3. @interface ViewController : UIViewController  
  4. {  
  5.     NSString *string;  
  6. }  
  7. @property  (nonatomic, retain) NSString *string;  
  8. @end  


打印结果是:

 

2012-07-19 20:42:08.113 TestProject1[1230:f803] str_Point:0x6d3b8f0  abcd  retainCount:1

2012-07-19 20:42:08.114 TestProject1[1230:f803] string_Point:0x6d3b8f0  abcd  retainCount:2

结果和上面copy一样。

 

注意:在IOS5之后,加入了Automatic Reference Counting (ARC),iOS5中新加了关键字有strong, weak, unsafe_unretained。