iOS - 区分NSDictionary和NSMutableDictionary? [英] iOS -- distinguish an NSDictionary from an NSMutableDictionary?

查看:83
本文介绍了iOS - 区分NSDictionary和NSMutableDictionary?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码片段:

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

所以如果我的对象是 NSDictionary NSMutableDictionary ,缺少try块(我宁愿避免),我该如何区分?

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?

推荐答案

NSAssert([bar isMemberOfClass: [NSMutableDictionary class]], @"");

这篇关于iOS - 区分NSDictionary和NSMutableDictionary?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆