I have the following code fragment:
NSString* value = @"value";
NSString* key = @"key";
NSMutableDictionary* foo = [NSMutableDictionary dictionary];
NSDictionary* bar = [NSDictionary dictionary];
NSAssert([bar isKindOfClass: [NSMutableDictionary class]], @""); // passes the assert as both are NSCFDictionary;
NSAssert([bar respondsToSelector: @selector(setValue:forKey:)], @""); // passes the assert because it NSCFDictionary does respond to the selector
[foo setValue:value forKey:key]; // no problem, foo is mutable
[bar setValue:value forKey:key]; // crash
So if I have an object which is either an NSDictionary or an NSMutableDictionary, short of a try block (which I’d rather avoid), how can I tell the difference?
1 Answer