//結論
對不能改變內容的物件(NSString, NSArray...)沒有差別,因為都是new一個新的;
對能改變內容的物件(NSMutableString, NSMutableArray...)差很多,如果assign是指到同一個記憶體位置,copy的話才會複製成另外一份。
//測試方法
NSString *a = @"a";
NSLog(@"1. a=%@",a);
NSString *b = [a copy];
NSString *c = a;
b = @"b";
a = @"c";
NSLog(@"2. a=%@ b=%@ c=%@", a, b, c);
NSMutableArray *aa = [@[@1,@2] mutableCopy];
NSLog(@"1. aa=%@",aa);
NSArray *bb = [aa copy];
NSMutableArray *cc = aa;
bb = @[@3,@4];
[cc addObject:@5];
NSLog(@"2. aa=%@ bb=%@ cc=%@", aa, bb, cc);
//執行結果
2014-05-22 17:26:25.285 ConceptComfirm[46837:60b] viewDidLoad
2014-05-22 17:26:25.286 ConceptComfirm[46837:60b] class : Concepts
2014-05-22 17:26:25.286 ConceptComfirm[46837:60b] 1. a=a
2014-05-22 17:26:25.286 ConceptComfirm[46837:60b] 2. a=c b=b c=a
2014-05-22 17:26:25.286 ConceptComfirm[46837:60b] 1. aa=(
1,
2
)
2014-05-22 17:26:25.286 ConceptComfirm[46837:60b] 2. aa=(
1,
2,
5
) bb=(
3,
4
) cc=(
1,
2,
5
)
2014-05-22 17:26:25.287 ConceptComfirm[46837:60b] viewWillAppear
2014-05-22 17:26:25.514 ConceptComfirm[46837:60b] viewDidAppear
沒有留言:
張貼留言