This might get down votes.... but gotta ask.
when "deallocating" a mutable dictionary, you call removeAllObjects
but can you just set it to nil
instead? Would that have the same result?
Like when you dealloc an array you set it to nil. Can I do that to a mutable array instead of calling removeAllObjects
?
First, ignore the absolute retainCount
. It is useless. See (the sadly gone web site here is the new one) http://sdarlington.github.io.
Secondly, the contents of the dictionary do not impact the lifespan of the dictionary itself (unless one of the objects in the dictionary has a strong reference to the dictionary, creating a retain cycle).
Thus removeAllObjects
is entirely orthogonal to the lifespan of the dictionary (retain cycle caveat does apply).
As well, when all strong references (ARC) to a dictionary are destroyed, the dictionary will be deallocated and, in that process, all hard references from the dictionary to the objects it contained will be released, too.
I.e.:
NSDictionary *dict = [NSDictionary dictionaryWithObjectAndKey:@(1),@"one"];
NSDictionary *doct = dict;
dict = nil; // nothing happens
[something consumeDict:doct];
dict = nil;
On that final dict = nil;
, the dictionary might be destroyed. Or not. It might not be destroyed immediately because consumeDict:
caused a strong reference somewhere or because consumeDict:
called retain
then autorelease
and the dictionary will stick around until the pool is drained.
Speaking of pools, sometimes, [NSDictionary dictionary]
returns an autoreleased object. So, sitting in the debugger, and checking whether the dictionary was released immediately after setting dict
to nil (maybe through a __weak reference) is going to lead to sorry. And note that +dictionary may sometimes return an autoreleased object or sometimes it won't depending on compiler optimization level or version.