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

Objective-C 對(duì)象復(fù)制 簡(jiǎn)單實(shí)現(xiàn)

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

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

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

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

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

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

這個(gè)兩個(gè)方法復(fù)制的對(duì)象都需要手動(dòng)釋放。

自義定義Class

自義定Class也需要實(shí)現(xiàn)NSCopying協(xié)義或NSMutableCopying協(xié)議后,其對(duì)象才能提供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 對(duì)象復(fù)制 簡(jiǎn)單實(shí)現(xiàn)的內(nèi)容介紹完,希望本文對(duì)你有所幫助!

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

2014-06-25 14:02:59

Objective-CKVO

2013-06-20 10:40:32

Objective-C實(shí)現(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-08-10 18:07:29

Objective-C反射

2011-06-17 17:27:29

Objective-CCocoa蘋果

2011-05-11 15:58:34

Objective-C

2013-03-27 12:54:00

iOS開發(fā)Objective-C

2011-05-11 11:20:26

Objective-C

2013-08-21 13:26:43

Objective-CNSDate說(shuō)明

2011-08-04 13:32:21

Objective-C 方法 對(duì)象

2011-08-04 11:04:14

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

2013-03-26 10:35:47

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

2011-08-02 13:16:36

Objective-C 語(yǔ)法 函數(shù)

2011-05-11 15:45:50

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

2013-08-21 14:57:42

objective-c問(wèn)題

2011-05-11 14:06:49

Objective-C

2011-08-04 14:58:37

Objective-C Cocoa NSString

2011-08-04 11:15:46

Objective-C 構(gòu)造函數(shù) 構(gòu)造方法
點(diǎn)贊
收藏

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