Objective-C 2.0屬性Property簡明教程
Objective-C 2.0屬性Property簡明教程是本文要介紹的內容,主要是來學習并了解Objective-C 2.0中的屬性。Objective-C 2.0 為我們提供了property。它大大簡化了我們創(chuàng)建數(shù)據(jù)成員讀寫函數(shù)的過程,更為關鍵的是它提供了一種更為簡潔,易于理解的方式來訪問數(shù)據(jù)成員。
我們先來看一下在Objective-C 1.x下我們聲明Book類的頭文件:
- //// Book.h #import <Cocoa/Cocoa.h>
- @interface Book : NSObject {
- NSString *title;
- NSNumber* numofpages;
- }
- - (id)initWithTitle:(NSString*) booktitle andNumofpages:(NSNumber*) num;
- - (NSString*) title;
- - (void) setTitle:(NSString*)newtitle;
- - (NSNumber*) numofpages;
- - (void) setNumofpages:(NSNumber*)newnumofpages;
- - (NSString*) summary;
- end
在Objective-C 2.0下,我們可以通過聲明與數(shù)據(jù)成員同名的property來省去讀寫函數(shù)的聲明。代碼如下所示:
- //// Book.h #import <Cocoa/Cocoa.h>
- @interface Book : NSObject {
- NSString *title;
- NSNumber* numofpages;
- }
- - (id)initWithTitle:(NSString*) booktitle andNumofpages:(NSNumber*) num;
- @property (retain) NSString* title;@property (retain) NSNumber* numofpages;
- @property (readonly) NSString* summary;
- @end
我們?yōu)槊恳粋€數(shù)據(jù)成員聲明了一個property。即使Book類中沒有summary這個數(shù)據(jù)成員,我們同樣可以聲明一個名為summary的property。聲明property的語法為:
@property (參數(shù)) 類型 名字;
這里的參數(shù)主要分為三類:讀寫屬性(readwrite/readonly),setter語意(assign/retain/copy)以及atomicity(nonatomic)。
assign/retain/copy決定了以何種方式對數(shù)據(jù)成員賦予新值。我們在聲明summary propery時使用了readonly,說明客戶端只能對該property進行讀取。atomicity的默認值是atomic,讀取函數(shù)為原子操作。
下面我們來看一下在Objective-C 1.x 下implementation文件:
- //// Book.m #import "Book.h"
- @implementation Book //
- @synthesize title;
- - (id)initWithTitle:(NSString*) booktitle andNumofpages:(NSNumber*) num{
- self = [super init];
- if(nil != self) {
- [self setNumofpages:num];
- [self setTitle:booktitle];
- }
- return self;
- }
- - (NSString*) title{
- return title;
- }
- - (void) setTitle:(NSString*)newtitle{
- [title release];
- title = [newtitle retain];
- }
- - (NSString*) description{
- return title;
- }
- - (NSNumber*) numofpages{
- return numofpages;
- }
- - (void) setNumofpages:(NSNumber*)newnumofpages{
- [numofpages release];
- numofpages = [newnumofpages retain];
- }
- -(NSString*) summary{
- NSString* retstr = [[NSString alloc]initWithFormat:@"Title: %@, Number of pages: %@",
- title, numofpages];
- [retstr autorelease];
- return retstr;
- }
- - (void) dealloc{
- [numofpages release];
- [title release];
- [super dealloc];
- }
- @end
在Objective-C 2.0下,由于我們聲明了property,implementation文件可以更改如下:
- //// Book.m #import "Book.h"
- @implementation Book
- @synthesize title;@synthesize numofpages;
- - (id)initWithTitle:(NSString*) booktitle andNumofpages:(NSNumber*) num{
- self = [super init];
- if(nil != self) {
- [self setNumofpages:num];
- [self setTitle:booktitle];
- }
- return self;
- }
- - (NSString*) description{ return title;
- }
- -(NSString*) summary{
- NSString* retstr = [[NSString alloc]initWithFormat:@"Title: %@, Number of pages: %@",
- title, numofpages];
- [retstr autorelease];
- return retstr;
- }
- - (void) dealloc{
- [numofpages release];
- [title release];
- [super dealloc];
- }
- @end
可以看到數(shù)據(jù)成員title和numofpages的讀寫函數(shù)已經(jīng)不復存在,取而代之的是兩行@synthesize,它讓編譯器在我們未提供讀寫函數(shù)時自動生成讀寫函數(shù)。
定義了property,客戶端可以使用book.title來取代[book title],這種語法比從前更加直觀簡潔。
實現(xiàn)文件中的16-17行代碼可修改如下:
- self.numofpages = num;
- self.title = booktitle
注意,許多人很容易忘記上面兩行代碼中的self,在這種情況下機器生成的讀寫函數(shù)并不會被調用,取而代之的是直接指針賦值,從而會引起內存泄露。
客戶端代碼如下所示:
- #import <Foundation/Foundation.h>
- #import "Book.h" int main (int argc, const char * argv[]) {
- NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
- NSString* name = [[NSString alloc] initWithString:@"Harry Porter"];
- NSNumber* number = [[NSNumber alloc] initWithInt:100];
- Book *book = [[Book alloc] initWithTitle:name andNumofpages:number];
- [number release];
- [name release];
- book.title = @"Twilight";
- book.numofpages = [NSNumber numberWithInt:200];
- NSString* str = book.summary;
- NSLog(@"summary: %@", str);
- [book release];
- [pool drain];
- return 0;
小結:Objective-C 2.0屬性Property簡明教程的內容介紹完了,希望通過本文的學習能對你有所幫助!