2013-05-09 61 views
0
NSMutableDictionary *dic0 = [[NSMutableDictionary alloc] initWithObjectsAndKeys:@"string0", @"key0", nil]; 
NSDictionary *dic1 = [[NSDictionary alloc] initWithObjectsAndKeys:@"string1", @"key1", nil]; 
NSDictionary *dic2 = [[NSDictionary alloc] initWithObjectsAndKeys:@"string2", @"key2", nil]; 
NSDictionary *dic3 = [[NSDictionary alloc] initWithObjectsAndKeys:@"dic3", @"key3", nil]; 

NSArray *arrayOri = [[NSArray alloc] initWithObjects:dic0, dic1, dic2, nil]; 

//here means a deep copy 
NSMutableArray *arrayDeepCopy = [[NSMutableArray alloc] initWithArray:arrayOri copyItems:YES]; 

NSRange range = {0, 2}; 
NSArray *subArray = [arrayOri subarrayWithRange:range]; 
[arrayDeepCopy addObject:dic3]; 

NSLog(@"arrayOri not merge %@", arrayOri); 

//merge one object 
[dic0 setObject:@"mutableV" forKey:@"mutableKey"]; 
//dealloc one object 
[dic1 dealloc]; 

NSLog(@"arrayOri %@ ", arrayOri); 
NSLog(@"subArray %@ ", subArray); 

墜毀在這裏,因爲DIC1的dealloced,如果深拷貝,爲什麼原來的對象有新對象的效果?initWithArray:(NSArray的*)陣列copyItems:(BOOL)標誌

NSLog(@"array %@ ", arrayDeepCopy); 

what initWithArray:(NSArray *)array copyItems:(BOOL)flag do after all?? 
+0

你需要更好地解釋你的問題。請不要只將代碼粘貼到評論中。這些很容易被忽略。用例子問一個問題,請閱讀[faq]和[about] – Liam 2013-05-09 07:33:40

回答

0

問題是,dic1是一個不可變的對象。因此將它複製到內存中是沒有意義的,因此arrayDeepCopy包含一個指向原始對象的指針dic1。當你釋放它時,它從存儲它的所有數組中消失。
如果你想有一個真正的深拷貝,你必須實例化一個NSMutableDictionary

+0

@ user2365237:我沒有得到任何迴應我的答案。所以,如果它解決了你的問題,請考慮接受它。如果仍有問題,請添加評論。 – 2013-05-12 14:50:45