2013-02-24 75 views
0

我試圖實施的NSMutableArray深和淺拷貝,如何在iOS中實現NSMutableArray的深度和淺度拷貝?

self.oldArray =[[NSMutableArray alloc] initWithCapacity:0]; 
self.shallowCopy =[[NSMutableArray alloc] initWithCapacity:0]; 
self.deepCopy =[[NSMutableArray alloc] initWithCapacity:0]; 


//add object to old Array 
[self.oldArray addObject:@"rooban"]; 
[self.oldArray addObject:@"radha"]; 
[self.oldArray addObject:@"jesus"]; 
[self.oldArray addObject:@"god"]; 


self.shallowCopy=[[NSMutableArray alloc] initWithArray:self.oldArray]; 
self.deepCopy=[[NSMutableArray alloc] initWithArray:self.oldArray copyItems:YES]; 

//deep copy 
NSLog(@"Lenght of the Old Array before: %d",self.deepCopy.count); 
[self.deepCopy removeObject:@"rooban"]; 
NSLog(@"Lenght of the Old Array After: %d",self.oldArray.count); 

//shallow copy 
NSLog(@"Lenght of the Old Array Before: %d",self.shallowCopy.count); 
[self.shallowCopy removeObject:@"rooban"]; 
NSLog(@"Lenght of the Old Array After: %d",self.oldArray.count); 

這個程序的輸出,

深副本:的4
長度:
前的舊陣列的Lenght舊數組後:4

淺拷貝:T 4
長度:
舊陣列之前的Lenght他舊陣列後:4

我不知道爲什麼淺反射覆制刪除不反映到原始NSMutableArray。

+0

初始化'deepCopy'和'shallowCopy'然後重新分配它們既浪費又可能是泄漏,如果你不使用ARC。 – 2013-02-24 09:14:50

回答

1

發生這種情況是因爲initWithArray:沒有執行淺拷貝。你得到一個全新的數組,其指針指向與另一個數組相同的對象。

E.g.如果添加的對象是可變字符串,則可以更改其中的一個,並且在新舊數組中它們都將可見,因爲它們指向相同的可變字符串。

另一方面,使用initWithArray:copyItems:創建的數組不會受到上述更改的影響,因爲它指向原始數組中的objets的副本。

+0

我可以知道我們如何在ios中實現淺和深的副本收集? – 2013-02-24 10:19:19

+0

@RoobanAbraham現在我想起來了,你在問題中看到的行爲不是淺拷貝的行爲嗎?但是,你期望它做的不是淺拷貝的行爲。要獲得您在問題中所討論的行爲,您只需創建另一個指向原始數組的指針。 – 2013-02-24 10:41:24

2

可以理解這樣的..

的NSMutableArray * oldArray = [[NSMutableArray裏的alloc] initWithCapacity:0]; NSMutableArray * shallowCopy; // = [NSMutableArray arrayWithCapacity:0]; NSMutableArray * deepCopy; // = [[NSMutableArray alloc] initWithCapacity:0];

//add object to old Array 
[oldArray addObject:@"rooban"]; 
[oldArray addObject:@"radha"]; 
[oldArray addObject:@"jesus"]; 
[oldArray addObject:@"god"]; 


shallowCopy = oldArray; 
deepCopy = [oldArray mutableCopy]; 

//deep copy 
NSLog(@"Lenght of the Old Array before: %d",deepCopy.count); 
[deepCopy removeObject:@"rooban"]; 
NSLog(@"Lenght of the Old Array After: %d",oldArray.count); 

//shallow copy 
NSLog(@"Lenght of the Old Array Before: %d",shallowCopy.count); 
[shallowCopy removeObject:@"rooban"]; 
NSLog(@"Lenght of the Old Array After: %d",oldArray.count); 

輸出是:

( - [視圖控制器viewDidLoad中])(ViewController.m:34)之前舊數組的長度:4 ( - [視圖控制器viewDidLoad中])(ViewController.m:36 )舊數組長度:4 ( - [ViewController viewDidLoad])(ViewController.m:39)舊數組長度:4 ( - [ViewController viewDidLoad])(ViewController。米:41)舊數組後的長度:3

概念爲淺拷貝和深拷貝是:

如果B是A的一個淺拷貝,那麼它是像B = [A ASSIGN] ;

B和A點到相同的存儲器位置

如果B是A的深層副本,那麼它是像B = [A拷貝];

B和A點到不同的存儲器位置

乙存儲器大小是與A相同的

B具有相同的內容A的

0

如果NSArray的/ NSDictionary中包含JSON Serialisable數據然後深拷貝可以使用NSJSONSerialization輕鬆實現。

使用NSJSONSerialization簡單地採取的NSArrayNSData然後重新創建JSON對象,這將創建的NSArray/NSDictionary一個完整的新的和新的副本與他們的新內存的引用。

但請確保NSArray/NSDictionary及其子項的對象必須是JSON可序列化的。

NSData *aDataOfSource = [NSJSONSerialization dataWithJSONObject:oldCopy options:NSJSONWritingPrettyPrinted error:nil]; 
NSDictionary *aDictNewCopy = [NSJSONSerialization JSONObjectWithData:aDataOfSource options:NSJSONReadingMutableLeaves error:nil];