Re: immutable NSMutableDictionary
Re: immutable NSMutableDictionary
- Subject: Re: immutable NSMutableDictionary
- From: Ali Ozer <email@hidden>
- Date: Mon, 17 Feb 2003 11:09:44 -0800
From: Jirtme Laurens <email@hidden>
Date: February 17, 2003 4:39:55 US/Pacific
To: email@hidden
Subject: immutable NSMutableDictionary
In a rather normal piece of code, I came to a weird thing. Assuming
myObject and myString are valid objects,
if([myDictionary isKindOfClass: [NSMutableDictionary class]])
{
[myDictionary setObject: myObject forKey: myKey];
}
when executing this code, I get an exception complaining that
myDictionary is immutable.
How can it be possible?
This can happen for instance if the mutable and immutable
implementations of a dictionary are represented by the same class,
which would have to represent itself as an NSMutableDictionary.
Checking the class inheritance for mutability is not a good technique.
For this reason, checking for implementation of setObject:forKey:
doesn't work either. In fact, there is no API or other way for
determining whether an NSDictionary (or NSArray or NSString) is
mutable; if you need to know, typically you would communicate this info
along with the collection.
Note that in almost all examples where you want to determine whether
something you got across an API call is mutable or not, it is usually a
bad idea to find out it's mutable and treat it mutably. The data
structure you got back might have been a shared or cached part of
something else, for instance. It's best to make decisions based on
the API itself.
Ali
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.