自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Objective-C 2.0屬性Property簡明教程

移動開發(fā) iOS
Objective-C 2.0屬性Property簡明教程是本文要介紹的內容,主要是來學習并了解Objective-C 2.0中的屬性。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類的頭文件:

  1. ////  Book.h #import <Cocoa/Cocoa.h>    
  2. @interface Book : NSObject {   
  3. NSString *title;   
  4. NSNumber* numofpages;  
  5. }   
  6. - (id)initWithTitle:(NSString*) booktitle andNumofpages:(NSNumber*) num;   
  7. - (NSString*) title;  
  8. - (void) setTitle:(NSString*)newtitle;   
  9. - (NSNumber*) numofpages;  
  10. - (void) setNumofpages:(NSNumber*)newnumofpages;   
  11. - (NSString*) summary;   
  12. end 

在Objective-C 2.0下,我們可以通過聲明與數(shù)據(jù)成員同名的property來省去讀寫函數(shù)的聲明。代碼如下所示:

  1. ////  Book.h #import <Cocoa/Cocoa.h>    
  2. @interface Book : NSObject {   
  3. NSString *title;   
  4. NSNumber* numofpages;  
  5. }   
  6. - (id)initWithTitle:(NSString*) booktitle andNumofpages:(NSNumber*) num;   
  7. @property (retain) NSString* title;@property (retain) NSNumber* numofpages;   
  8. @property (readonly) NSString* summary;   
  9. @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文件:

  1.  ////  Book.m #import "Book.h"    
  2.  @implementation Book //  
  3.  @synthesize title;   
  4.  - (id)initWithTitle:(NSString*) booktitle andNumofpages:(NSNumber*) num{   
  5.  self = [super init];   
  6.  if(nil != self) {   
  7.   [self setNumofpages:num];    
  8.   [self setTitle:booktitle];   
  9.   }   
  10.  return self;  
  11. }   
  12. - (NSString*) title{   
  13. return title;  
  14. }   
  15. - (void) setTitle:(NSString*)newtitle{   
  16. [title release];   
  17. title = [newtitle retain];  
  18. }   
  19. - (NSString*) description{   
  20. return title;  
  21. }   
  22.  - (NSNumber*) numofpages{   
  23.  return numofpages;  
  24.  }   
  25.  - (void) setNumofpages:(NSNumber*)newnumofpages{   
  26.  [numofpages release];   
  27.  numofpages = [newnumofpages retain];  
  28.  }  
  29.   -(NSString*) summary{   
  30.   NSString* retstr = [[NSString alloc]initWithFormat:@"Title: %@, Number of pages: %@",  
  31.                           title, numofpages];   
  32.             [retstr autorelease];   
  33.             return retstr;  
  34.         }   
  35.    - (void) dealloc{   
  36.    [numofpages release];   
  37.    [title release];   
  38.    [super dealloc];  
  39. }   
  40. @end 

在Objective-C 2.0下,由于我們聲明了property,implementation文件可以更改如下:

  1.  ////  Book.m #import "Book.h"    
  2.  @implementation Book   
  3.  @synthesize title;@synthesize numofpages;   
  4.  - (id)initWithTitle:(NSString*) booktitle andNumofpages:(NSNumber*) num{   
  5.       self = [super init];   
  6.       if(nil != self) {    
  7.       [self setNumofpages:num];    
  8.       [self setTitle:booktitle];   
  9.    }   
  10.    return self;  
  11.  }   
  12.  - (NSString*) description{ return title;  
  13. }   
  14. -(NSString*) summary{   
  15. NSString* retstr = [[NSString alloc]initWithFormat:@"Title: %@, Number of pages: %@",    
  16.                                    title, numofpages];   
  17.          [retstr autorelease];   
  18.    return retstr;  
  19.  }   
  20. - (void) dealloc{   
  21. [numofpages release];   
  22. [title release];   
  23. [super dealloc];  
  24. }   
  25. @end 

可以看到數(shù)據(jù)成員title和numofpages的讀寫函數(shù)已經(jīng)不復存在,取而代之的是兩行@synthesize,它讓編譯器在我們未提供讀寫函數(shù)時自動生成讀寫函數(shù)。

定義了property,客戶端可以使用book.title來取代[book title],這種語法比從前更加直觀簡潔。

實現(xiàn)文件中的16-17行代碼可修改如下:

  1. self.numofpages = num;  
  2. self.title = booktitle 

注意,許多人很容易忘記上面兩行代碼中的self,在這種情況下機器生成的讀寫函數(shù)并不會被調用,取而代之的是直接指針賦值,從而會引起內存泄露。

客戶端代碼如下所示:

  1. #import <Foundation/Foundation.h> 
  2. #import "Book.h" int main (int argc, const char * argv[]) {  
  3.  NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];    
  4.  NSString* name = [[NSString alloc] initWithString:@"Harry Porter"];   
  5.  NSNumber* number = [[NSNumber alloc] initWithInt:100];   
  6.  Book *book = [[Book alloc] initWithTitle:name andNumofpages:number];   
  7.  [number release];   
  8.  [name release];    
  9.  book.title = @"Twilight";   
  10.  book.numofpages = [NSNumber numberWithInt:200];   
  11.  NSString* str = book.summary;   
  12.  NSLog(@"summary: %@", str);   
  13.  [book release];    
  14.  [pool drain];      
  15.  return 0;  

小結:Objective-C 2.0屬性Property簡明教程的內容介紹完了,希望通過本文的學習能對你有所幫助!

責任編輯:zhaolei 來源: 互聯(lián)網(wǎng)
相關推薦

2011-08-17 09:55:45

Objective-CCategory

2013-05-02 10:51:17

iOS開發(fā)Objective-C@property

2013-12-03 13:05:30

Lua腳本語言

2011-07-08 13:49:46

Objective-C UUID

2011-07-19 17:18:35

Objective-C Property

2009-08-06 17:45:08

C# Webservi

2011-08-01 11:37:41

iPhone Objective- 內存

2011-08-17 15:37:23

Objective-C垃圾收集

2009-09-02 17:38:19

C#開發(fā)GIS

2011-07-27 17:10:30

Objective-C 持久化

2011-08-05 14:03:39

Objective-C 對象 模板

2014-06-20 10:51:35

Linux LVM邏輯卷

2011-08-22 09:48:16

WindowsObjective-C

2011-06-03 08:49:54

Java

2011-08-16 10:23:04

Objective-CNSAutoreleaXcode常用鍵

2011-07-29 16:08:31

Objective-C 內存

2023-10-20 14:08:35

digDNS

2011-07-19 15:15:09

Objective-C 內存

2011-07-25 17:31:49

iPhone Objective-

2011-08-10 18:07:29

Objective-C反射
點贊
收藏

51CTO技術棧公眾號