偷偷摘套内射激情视频,久久精品99国产国产精,中文字幕无线乱码人妻,中文在线中文a,性爽19p

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

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

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

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

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

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

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

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

自義定義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.   //深復制  NSMutableString *tmpStr = [interest mutableCopy];    
  35.   newObj.interest = tmpStr;    
  36.   [tmpStr release];      
  37.   //淺復制  //newObj.  
  38.   interestinterest = interest;    
  39.   return newObj;  
  40.   }  
  41.   @end 

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

責任編輯:zhaolei 來源: 博客園
相關推薦

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面向對象內存

2011-07-27 16:55:12

Objective-c 閉包

2011-08-17 11:05:22

Objective-C方法

2011-08-10 18:07:29

Objective-C反射

2013-03-27 12:54:00

iOS開發(fā)Objective-C

2011-05-11 15:58:34

Objective-C

2011-05-11 11:20:26

Objective-C

2011-06-17 17:27:29

Objective-CCocoa蘋果

2013-08-21 13:26:43

Objective-CNSDate說明

2011-08-04 11:04:14

Objective-C 面向對象 繼承

2011-08-04 13:32:21

Objective-C 方法 對象

2013-03-26 10:35:47

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

2011-05-11 13:54:08

Objective-C

2011-05-11 15:45:50

內存管理Objective-C

2011-08-02 13:16:36

Objective-C 語法 函數(shù)

2011-08-04 11:15:46

Objective-C 構造函數(shù) 構造方法

2011-05-11 14:06:49

Objective-C

2011-08-04 14:58:37

Objective-C Cocoa NSString
點贊
收藏

51CTO技術棧公眾號