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

Objective-C 對象復(fù)制 簡單實現(xiàn)

移動開發(fā) iOS
本文介紹的是Objective-C 對象復(fù)制 簡單實現(xiàn),以簡單的代碼實現(xiàn)簡單的功能,那么我們來看內(nèi)容。

Objective-C 對象復(fù)制 簡單實現(xiàn)是本文要介紹的內(nèi)容,也行對Objective-C 也不算陌生了,我們先來看內(nèi)容。

Foundation系統(tǒng)對象(NSString,NSArray等)

只有遵守NSCopying 協(xié)議的類才可以發(fā)送copy消息

只有遵守 NSMutableCopying 協(xié)議的類才可以發(fā)送mutableCopy消息

copy和mutableCopy區(qū)別就是copy返回后的是不能修改的對象, 而mutableCopy返回后是可以修改的對象

這個兩個方法復(fù)制的對象都需要手動釋放。

自義定義Class

自義定Class也需要實現(xiàn)NSCopying協(xié)義或NSMutableCopying協(xié)議后,其對象才能提供copy功能。代碼

  1. //TestProperty.h  
  2. #import <Cocoa/Cocoa.h> 
  3. @interface TestProperty : NSObject <NSCopying>{    
  4. NSString *name;  NSString *password;   
  5.  NSMutableString *interest;    
  6.  NSInteger myInt;}@property (retain,nonatomic)   
  7.  NSString *name,*password;  
  8.  @property (retain,nonatomic) NSMutableString *interest;  
  9.  @property NSInteger myInt;  
  10.  -(void) rename:(NSString *)newname;  
  11.  @end//TestProperty.m  
  12.  #import "TestProperty.h"  
  13.  @implementation TestProperty  
  14.  @synthesize name,password,interest;  
  15.  @synthesize myInt;  
  16.  -(void) rename:(NSString *)newname{    
  17.  // 這里可以直接寫成    
  18.  // self.name = newname;    
  19.  //  if (name != newname) {      
  20.  [name autorelease];      
  21.  name = newname;     
  22.  [name retain];    
  23.  }  
  24.  }  
  25.  -(void) dealloc{    
  26.  self.name = nil;    
  27.  self.password = nil;    
  28.  self.interest = nil;   
  29.   [super dealloc];}- (id)copyWithZone:(NSZone *)zone{    
  30.   TestProperty *newObj = [[[self class] allocWithZone:zone] init];    
  31.   newObj.name = name;    
  32.   newObj.password = password;    
  33.   newObj.myInt = myInt;    
  34.   //深復(fù)制  NSMutableString *tmpStr = [interest mutableCopy];    
  35.   newObj.interest = tmpStr;    
  36.   [tmpStr release];      
  37.   //淺復(fù)制  //newObj.  
  38.   interestinterest = interest;    
  39.   return newObj;  
  40.   }  
  41.   @end 

小結(jié):Objective-C 對象復(fù)制 簡單實現(xiàn)的內(nèi)容介紹完,希望本文對你有所幫助!

責(zé)任編輯:zhaolei 來源: 博客園
相關(guān)推薦

2014-06-25 14:02:59

Objective-CKVO

2013-06-20 10:40:32

Objective-C實現(xiàn)截圖

2011-08-11 16:01:03

Objective-C面向?qū)ο?/a>內(nèi)存

2011-07-27 16:55:12

Objective-c 閉包

2011-08-17 11:05:22

Objective-C方法

2011-06-17 17:27:29

Objective-CCocoa蘋果

2011-08-10 18:07:29

Objective-C反射

2013-03-27 12:54:00

iOS開發(fā)Objective-C

2011-05-11 11:20:26

Objective-C

2011-05-11 15:58:34

Objective-C

2013-08-21 13:26:43

Objective-CNSDate說明

2011-08-04 11:04:14

Objective-C 面向?qū)ο? 繼承

2011-08-04 13:32:21

Objective-C 方法 對象

2013-03-26 10:35:47

Objective-C單例實現(xiàn)

2011-08-02 13:16:36

Objective-C 語法 函數(shù)

2011-08-04 14:58:37

Objective-C Cocoa NSString

2011-05-11 13:54:08

Objective-C

2011-05-11 15:45:50

內(nèi)存管理Objective-C

2013-08-21 14:57:42

objective-c問題

2011-05-11 14:06:49

Objective-C
點贊
收藏

51CTO技術(shù)棧公眾號