The method retainCount is supposed to return an unsigned integer.
Why then, does [@"Hi" retainCount] return -1?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The simple answer is that because
@"Hi” is a string literal, it will always be sitting around in the binary executable image, and will never “go away”, thus retain/release have no effect, and you’re seeingUINT_MAX(which looks like -1 when printed out signed eg with %d). (See Pete Kirkham’s answer about NSObjects having these semantics).Beyond this, it’s useful to know that although @”Hi” behaves like an
NSString*, it’s actually a compiler-created instance of the classCFConstantString(or possibly NSConstantString, my debugger doesn’t agree with some documentation), which wraps the literal string data and gives you the NSString* interface on it. But the compiler knows that these strings are special and can’t ever get cleaned up, so they will always have a retainCount of UINT_MAX (-1)