標籤:
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
- #import <UIKit/UIKit.h>
-
- @interface ViewController : UIViewController
- {
- NSObject *obj;
- }
- @end
[cpp] view plaincopy
- - (void)viewDidLoad
- {
- [super viewDidLoad];
- self.obj = nil;、
- }
提示不可用。
加上property
[cpp] view plaincopy
- #import <UIKit/UIKit.h>
-
- @interface ViewController : UIViewController
- {
- NSObject *obj;
- }
- @property (nonatomic,retain) NSObject *obj;
- @end
編譯能通過,運行,崩潰,提示錯誤 reason: ‘-[ViewController setObj:]: unrecognized selector sent to instance 0x6b6c480
那就是我們沒事實現setter方法。
用@synthesize關鍵字實現getter 和setter。
在.m檔案中
[cpp] view plaincopy
- @implementation ViewController
- @synthesize obj;
- - (void)viewDidLoad
- {
- [super viewDidLoad];
- self.obj = nil;
- }
運行,程式運行正常。說明setter 起作用了。
3、@property和@synthesize關鍵字 產生的程式碼
到底@property和@synthesize關鍵字產生了什麼代碼呢?我們自己實現getter 和setter也可以替代這些關鍵字。
把這兩個關鍵字對應的代碼注釋掉
.h
[cpp] view plaincopy
- #import <UIKit/UIKit.h>
-
- @interface ViewController : UIViewController
- {
- NSObject *obj;
- }
- //@property (nonatomic,retain) NSObject *obj;
- -(NSObject*)obj;
- -(void)setObj:(NSObject*)newObj;
- @end
.m
[cpp] view plaincopy
- @implementation ViewController
- //@synthesize obj;
- - (void)viewDidLoad
- {
- [super viewDidLoad];
- self.obj = nil;
- }
-
- -(NSObject*)obj{
- return obj;
- }
- -(void)setObj:(NSObject*)newObj{
- if(obj != newObj){
- [obj release];
- obj = [newObj retain];
- }
- }
再運行,也能正常啟動。說明自己寫的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
- #import <UIKit/UIKit.h>
-
- @interface ViewController : UIViewController
- {
- NSString *string;
- }
- @property (nonatomic, copy) NSString *string;
- @end
.m檔案
[cpp] view plaincopy
- @synthesize string;
- - (void)viewDidLoad
- {
- [super viewDidLoad];
-
- NSString *str = [[NSString alloc] initWithFormat:@"abcd"];
- NSLog(@"str_Point:%p %@ retainCount:%d", str, str, [str retainCount]);
- self.string = str;
- NSLog(@"string_Point:%p %@ retainCount:%d", string, string, [string retainCount]);
- }
列印結果
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
- #import <UIKit/UIKit.h>
-
- @interface ViewController : UIViewController
- {
- NSString *string;
- }
- @property (nonatomic, retain) NSString *string;
- @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詳解