是否有 NSMutableDictionary 文字语法来删除元素? [英] Is there NSMutableDictionary literal syntax to remove an element?
本文介绍了是否有 NSMutableDictionary 文字语法来删除元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在 NSMutableDictionary 中有添加对象和更改对象的字面语法,是否有删除对象的字面语法?
There is a literal syntax to add object and change object in an NSMutableDictionary, is there a literal syntax to remove object?
推荐答案
是的,但是... :-)
Yes, but... :-)
默认情况下不支持此功能,但是用于设置字典元素的新语法使用方法 setObject:forKeyedSubscript:
而不是 setObject:forKey:
.因此,您可以编写一个类别来替换前者并设置或删除元素:
This is not supported by default, however the new syntax for setting dictionary elements uses the method setObject:forKeyedSubscript:
rather than setObject:forKey:
. So you can write a category which replaces the former and either sets or removes the element:
@implementation NSMutableDictionary (RemoveWithNil)
- (void) setObject:(id)obj forKeyedSubscript:(id<NSCopying>)key
{
if (obj)
[self setObject:obj forKey:key];
else
[self removeObjectForKey:key];
}
@end
将其添加到您的应用程序中,然后:
Add that to your application and then:
dict[aKey] = nil;
将删除一个元素.
这篇关于是否有 NSMutableDictionary 文字语法来删除元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文