我创建了一个字典:
var myDictionary = [String: [NSDate, String]]()
我正在尝试在字典中设置值:
datesDictionary.updateValue([NSDate(), "string1"] , forKey: "string2")
我遇到编译器错误:
Cannot invoke 'updateValue' with an argument list of type '([NSObject], forKey: String)'
怎么了?关于字典,我缺少什么?我没有将其设置为可选。
最佳答案
我猜你不需要数组,如果你知道数组中每个对象的类型并且数组很短。 在 swift 中我认为这样更好
var myDictionary = [String: (NSDate, String)]()
myDictionary.updateValue((NSDate(), "string1") , forKey: "string2")
println(myDictionary)
即使你接受了我的回答,我也想指出,在你的代码中,你定义了一个像这样的字典 [String : [(NSDate, String)]]
所以,你的键是String
,值是Array[(NSDate, String)]
,这个数组中每个对象的类型是(NSDate, String)
所以,如果你想让你的代码正常工作,你必须这样做
var myDic2 = [String:[NSDate,String]]()
myDic2.updateValue([(NSDate(),"string2")], forKey: "string1")
println(myDic2)
关于ios - 无法使用类型为 ( ['NSObject' )、forKey : String) 的参数列表调用 updateValue,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30207624/