[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Problems with Replacing a String is a NSMutableString
From: |
Helge Hess |
Subject: |
Re: Problems with Replacing a String is a NSMutableString |
Date: |
Thu, 24 Jul 2008 10:41:40 +0200 |
On 24.07.2008, at 10:20, Charles philip Chan wrote:
Keys for collections are copied
^^^ ^^^^^
by the collection to insure they are immutable. Allowing them to be
mutable would break the internal hash lookups.
I still don't understand this
...
| Returns an array containing all the dictionary's keys that are
^^^^
| associated with anObject.[1]
`----
So basically I am returning an array of the keys, and since there is
only 1 key for the object "Album" I search for it at index zero in the
array and return it as an NSMutableString.
Dictionary keys are never NSMutableString objects. As David says, the
NSMutableString will get copied when its used as a dictionary key.
NSString *key = [NSMutableString stringWithString:@"bla"];
[yourDict setObject:@"this and that" forKey:key];
// the yourDict has no reference to your 'key' object,
// it made a copy
Aproximately this happens from a copy-perspective:
NSString *key = [NSMutableString stringWithString:@"bla"];
NSString *dictCopy;
dictCopy = [key copy];
[yourDict setObject:@"this and that" forKey:dictCopy];
[dictCopy release];
As mentioned by David a mutable object can't be used as a hashtable
key for rather obvious reasons :-) (if not, lookup hashtable in
Wikipedia :-)
NSMutableDictionary ensures the immutability by calling -copy.
Helge
--
Helge Hess
http://helgehess.eu/