views:

66

answers:

1

Is it ok to cast a NSdictionary object to NSMutableDictionary?

NSDictionary *dict;
NSMutableDictionary *mDict = (NSMutableDictionary *)dict;

Tried this and it worked fine.

+3  A: 

No it is not. If you want a mutable dictionary, make one:

NSMutableDictionary* mutableDict = [dict mutableCopy];

Do not forget in accordance with the rules you own mutableDict and must release it when you are done with it.

jer
what's wrong with the code in my question?
Fasid
You can also easily use [NSMutableDictionary dictionaryWithDictionary:myOriginalDictionary]; which will return an autoreleased object.
Brad Goss
It's wrong to assume it's a mutable dictionary... If it isn't, then you could potentially send the object methods that the class does not respond to, which will crash your program. It's better to be safe.
Brad Goss
thanks i understand now
Fasid