Objective-C 2.0属性Property简明教程

移动开发 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。它大大简化了我们创建数据成员读写函数的过程,更为关键的是它提供了一种更为简洁,易于理解的方式来访问数据成员。

我们先来看一下在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下,我们可以通过声明与数据成员同名的property来省去读写函数的声明。代码如下所示:

  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 

我们为每一个数据成员声明了一个property。即使Book类中没有summary这个数据成员,我们同样可以声明一个名为summary的property。声明property的语法为:

@property (参数) 类型 名字;

这里的参数主要分为三类:读写属性(readwrite/readonly),setter语意(assign/retain/copy)以及atomicity(nonatomic)。

assign/retain/copy决定了以何种方式对数据成员赋予新值。我们在声明summary propery时使用了readonly,说明客户端只能对该property进行读取。atomicity的默认值是atomic,读取函数为原子操作。

下面我们来看一下在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 

可以看到数据成员title和numofpages的读写函数已经不复存在,取而代之的是两行@synthesize,它让编译器在我们未提供读写函数时自动生成读写函数。

定义了property,客户端可以使用book.title来取代[book title],这种语法比从前更加直观简洁。

实现文件中的16-17行代码可修改如下:

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

注意,许多人很容易忘记上面两行代码中的self,在这种情况下机器生成的读写函数并不会被调用,取而代之的是直接指针赋值,从而会引起内存泄露。

客户端代码如下所示:

  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 来源: 互联网
相关推荐

2011-08-17 09:55:45

Objective-CCategory

2013-05-02 10:51:17

iOS开发Objective-C@property

2011-07-19 17:18:35

Objective-C Property

2011-07-08 13:49:46

Objective-C UUID

2013-12-03 13:05:30

Lua脚本语言

2009-08-06 17:45:08

C# Webservi

2011-08-01 11:37:41

iPhone Objective- 内存

2011-08-17 15:37:23

Objective-C垃圾收集

2011-07-27 17:10:30

Objective-C 持久化

2011-08-05 14:03:39

Objective-C 对象 模板

2009-09-02 17:38:19

C#开发GIS

2014-06-20 10:51:35

Linux LVM逻辑卷

2011-08-22 09:48:16

WindowsObjective-C

2011-08-16 10:23:04

Objective-CNSAutoreleaXcode常用键

2011-07-29 16:08:31

Objective-C 内存

2011-06-03 08:49:54

Java

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技术栈公众号