views:

133

answers:

1

Can someone explain why the last line prints out -1? It happens when copy is called on a NSMutableString, I expect the returnCount of strFour to be 1, since an immutable copy should be returned.

    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

Thanks a lot.

+3  A: 
dreamlax
Thanks dreamlax
Michael Z