For NSMutableString AND NSString
say we have:
1 |
NSMutableString * temp = [[NSMutableString alloc] initWithFormat:@"hahaha"]; |
If you use
1 |
NSArray * copyArray = [temp copy] |
it will return you an immutable object (NSString*) with its own address. This means you cannot change the data.
Hence, even if you declare a NSMutableString pointer, and use setString to change the data, the compiler will give you an exception.
1 2 3 |
NSString * shallowCopy = [temp copy]; //or NSMutableString * shallowCopy = [temp copy]; |
if you use [temp mutableCopy], it will return an mutable object (NSMutableString*) with its own address.
However, be sure that you declare a NSMutableString * so that you can use setString to change the data and see the difference.
1 2 3 4 5 6 |
NSMutableString * temp = [[NSMutableString alloc] initWithFormat:@"hahaha"]; NSLog(@"address of temp: %p", &temp); NSMutableString * shallowCopy = [temp copy]; //returns immutable object, has own address, same data NSLog(@"address of shallowCopy: %p", &shallowCopy); NSMutableString * mutableCopy = [temp mutableCopy]; //returns mutable (changeable) object, own address NSLog(@"address of mutableCopy: %p", &mutableCopy); |