do *_*ter 63 dictionary swift swift2
如果可能的话,我想从字典中删除一个键值对.
var dict: Dictionary<String,String> = [:]
var willRemoveKey = "SomeKey"
dict.removePair(willRemoveKey) //that's what I need
Run Code Online (Sandbox Code Playgroud)
Kam*_*xom 133
你可以用这个:
dict[willRemoveKey] = nil
Run Code Online (Sandbox Code Playgroud)
或这个:
dict.removeValueForKey(willRemoveKey)
Run Code Online (Sandbox Code Playgroud)
唯一的区别是第二个将返回删除的值(如果不存在则返回nil)
dict.removeValue(forKey: willRemoveKey)
Run Code Online (Sandbox Code Playgroud)
dict.removeValueForKey(willRemoveKey)
Run Code Online (Sandbox Code Playgroud)
或者您可以使用下标语法:
dict[willRemoveKey] = nil
Run Code Online (Sandbox Code Playgroud)