There's pretty much never a good reason to use -retainCount
. Here's a short and mildly abusive explanation why.
No.
Still no.
Did it, by any chance, look like this?
while ([a retainCount] > 0) { [a release]; }
Good luck with that. Please let me know which app so I can avoid buying it.
Perhaps. But probably for the wrong reasons. And it won’t take much to break it.
If you’re going to keep using -retainCount
after the advice of lots of knowledgeable and influential people, including the people who wrote the framework, I reserve the right to.
-retainCount
any more.See the above comment about Apple recommending against using -retainCount
.
Basically it doesn’t say what you think it does. Or rather, it does but it’s never accurate in any non-trivial case.
For example:
[NSNumber numberWithInt:1]
would have a retain count of 1. It doesn't. It's 2.@"Foo"
would have a retain count of 1. It doesn't. It's 1152921504606846975.[NSString stringWithString:@"Foo"]
would have a retain count of 1. It doesn't. Again, it's 1152921504606846975.(H/T to Dave DeLong for these.)
Still no.
Did you ever use autorelease? The value of retainCount includes only the current retain count. You have no idea whether it has been autoreleased. If you add a release now it might release again when the autorelease pool drains.
If you use ARC, the simple answer is that you don't. (Unless you use Foundation classes, in which case read on.)
You need to understand your code.
It’s pretty straightforward. If you alloc, copy, new or retain an object you need to release it. Otherwise you don’t.
As I say, let me know which app you’re using so I can not buy it.
But first, have a look at the following resources. Hopefully they’ll change your mind.
I'm Stephen Darlington. You can follow me on Twitter at @sdarlington or check out my apps at Wandle Software. All code was written without the aid of -retainCount
.