When you add an object to a collection, for example NSMutableArray, the object is copied, i.e. semantics of meaning or persisted, that is, referential semantics?
I am confused in the example:
NSMutableString *testStr = [@"test" mutableCopy];
NSMutableArray *arrayA = [[NSMutableArray alloc] init];
[arrayA addObject:testStr];
NSLog(@"%@", arrayA);
testStr = [@"world" mutableCopy];
NSLog(@"%@", arrayA);
NSMutableArray *testArr = [@[@1, @2] mutableCopy];
NSMutableArray *arrarB = [[NSMutableArray alloc] init];
[arrarB addObject:testArr];
NSLog(@"%@", arrarB);
[testArr addObject:@3];
NSLog(@"%@", arrarB);
You can see: if the object is NSMutableString, it looks like a copied object - you change the object, it will not affect the object in the array.
However, if the object is NSMutableArray, when the object is changed, the object in the array also changes - for example, you save the object or pass by reference.
Am I missing something? Thanks.
source
share