NSString *str = @"hello";
NSLog(@"Pointer = %p", str);
[self doSomthingWithString:&str];
- (void)doSomthingWithString:(NSString **)str {
*str = @"world";
NSLog(@"Pointer = %p", *str);
}
也许这是一个明显的问题,但我希望打印出相同的地址:
Pointer = 0x104ce9f40
Pointer = 0x104ce9fa0
答案 0 :(得分:1)
不,您不应该看到打印的地址相同,因为doSomthingWithString
会从调用方向str
分配一个新对象。它不修改NSString
。
分配NSString
后,"Hello"
引用的内容为str
的{{1}}不再被引用,因此打印输出不同。
也许更好的例子是使用*str = @"world"
的单独字符串,如下所示:
"world"