#import <Foundation/Foundation.h>
int main(int argc, const char * argv[]) {
@autoreleasepool {
NSString *hello = @"hello world";
NSString *world = hello;
NSLog(@"1. %p =? %p", hello, world);
NSString *hi = @"hello world";
NSLog(@"2. %p =? %p",hello, hi);
NSString *hei = [NSString stringWithFormat:@"%@", @"hello world"];
NSLog(@"3. %p =? %p", hello, hei); //采用类实例化的常量字符串是不同的对象
NSMutableString *mutableHello = [NSMutableString stringWithString:hello];
NSLog(@"4. %p =? %p", hello, mutableHello);
NSString *hellocopy = [hello copy];
NSLog(@"5. %p =? %p", hello, hellocopy); //同一个对象的两份引用
NSMutableString *helloMutableCopy = [hello mutableCopy];
NSLog(@"6. %p =? %p", hello, helloMutableCopy); //终于产生新对象了
NSString *mutableHelloCopy = [mutableHello copy];
NSLog(@"7. %p =? %p", mutableHello, mutableHelloCopy); //我们都是新对象
NSString *mutableHelloMutableCopy = [mutableHello mutableCopy];
NSLog(@"8. %p =? %p", mutableHello, mutableHelloMutableCopy); //我们都是新对象
}
return 0;
}
/*
2015-12-10 10:32:32.098 Interview[3940:264604] 1. 0x100001048 =? 0x100001048
2015-12-10 10:32:32.099 Interview[3940:264604] 2. 0x100001048 =? 0x100001048
2015-12-10 10:32:32.099 Interview[3940:264604] 3. 0x100001048 =? 0x1007000e0
2015-12-10 10:32:32.099 Interview[3940:264604] 4. 0x100001048 =? 0x1007000a0
2015-12-10 10:32:32.099 Interview[3940:264604] 5. 0x100001048 =? 0x100001048
2015-12-10 10:32:32.099 Interview[3940:264604] 6. 0x100001048 =? 0x1007002a0
2015-12-10 10:32:32.099 Interview[3940:264604] 7. 0x1007000a0 =? 0x1001067f0
2015-12-10 10:32:32.099 Interview[3940:264604] 8. 0x1007000a0 =? 0x100106810
Program ended with exit code: 0
*/