【轉】Objective-C文法property詳解

來源:互聯網
上載者:User

標籤:

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直接賦值,而不是複製或者保留它。適用於基礎資料型別 (Elementary Data Type),比如NSInteger和CGFloat,或者你並不直接擁有的類型,比如delegates。

2、如何使用property1.1  沒有property和有property的對比

在標頭檔定義 obj。在.m檔案中使用

 

[cpp] view plaincopy 
  1. #import <UIKit/UIKit.h>  
  2.   
  3. @interface ViewController : UIViewController  
  4. {  
  5.     NSObject *obj;  
  6. }  
  7. @end  
[cpp] view plaincopy 
  1. - (void)viewDidLoad  
  2. {  
  3.     [super viewDidLoad];      
  4.     self.obj = nil;、  
  5. }  

提示不可用。

加上property

 

 

[cpp] view plaincopy 
  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 plaincopy 
  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 plaincopy 
  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 plaincopy 
  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 number;        

@property int number;  

對int 來說,atomic assign都是預設行為,所以這三行是等價的。

 

@property  NSObject *obj;這樣寫行嗎?不行的,警示告


只有int 等基礎資料類型能這麼寫。對象必須加上賦值的類型。

 

@property  (retain) NSObject *obj;這樣就沒問題了。何時使用assign、何時使用retain、copy後面再講。

5、retain和copy實驗。

使用copy。

.h檔案

 

[cpp] view plaincopy 
  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 plaincopy 
  1. @synthesize string;  
  2. - (void)viewDidLoad  
  3. {  
  4.     [super viewDidLoad];  
  5.       
  6.     NSString *str = [[NSString alloc] initWithFormat:@"abcd"];  
  7.     NSLog(@"str_Point:%p  %@  retainCount:%d", str, str, [str retainCount]);  
  8.     self.string = str;  
  9.     NSLog(@"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 plaincopy 
  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。

著作權聲明:本文由http://blog.csdn.net/totogo2010/原創,歡迎轉載分享。請尊重作者勞動,轉載時保留該聲明和作者部落格連結,謝謝!

【轉】Objective-C文法property詳解

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.