大家好,我是 swift 的新手,在我的应用程序中,我声明了一个这样的字典:
var imageDict : Dictionary<String, String> = [:]
我想像这样为该字典设置值:
imageDict.setValue(NSStringFromCGPoint(frame), forKey: NSString.stringWithString("/(tagValue)"));
但是我得到了这样的错误:
Dictonary <String, String> does not have a member named 'setValue'
这个问题来 self 之前的问题,谁能解释我为什么不能为该词典设置值,谁能告诉我任何其他方法吗?
提前致谢。
最佳答案
Swift 字典没有像 setValue:forKey: 这样的方法。只有 NSMutableDictionary 有这样的方法。如果你想在 swift 中为键赋值,你应该使用下标。如果你想用 swift 字典来做,这是正确的方法。
var imageDict:[String: String] = [:]
imageDict["\(tagValue)"] = NSStringFromCGRect(frame)
或者如果你想使用 NSMutableDictionary,它看起来像这样,
var imageDict = NSMutableDictionary()
imageDict.setObject(NSStringFromCGRect(frame), forKey: "\(tagValue)")
关于swift - 为什么我不能对字典使用 SetValue?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26313847/