有人可以解释为什么最后一行打印出-1吗?当在 NSMutableString 上调用 copy 时会发生这种情况,我希望 strFour 的 returnCount 为 1,因为应该返回一个不可变的副本。
NSMutableString *str =[NSMutableString stringWithString:@"hi"];
NSLog(@"new instantiated variable has a retain cout:");
NSLog(@"%d",[str retainCount]); //1, str is a pointer, its value is a memory address
NSMutableString *strFour =[str copy]; //receiver str is mutable, copy returns an immutable
NSLog(@"%d",[strFour retainCount]); ////strFour s retain count should be 1, but it had a retain count of -1
非常感谢。