为什么我不能将SetValue用于Dictionary?

Dha*_*esh 7 swift xcode6

大家好我是swift的新手,在我的应用程序中我声明了这样的字典:

var imageDict : Dictionary<String, String> = [:]
Run Code Online (Sandbox Code Playgroud)

我想像这样设置该字典的值:

imageDict.setValue(NSStringFromCGPoint(frame), forKey: NSString.stringWithString("/(tagValue)"));
Run Code Online (Sandbox Code Playgroud)

但我得到这样的错误:

Dictonary <String, String> does not have a member named 'setValue'
Run Code Online (Sandbox Code Playgroud)

这个问题来自我之前的问题,并且可以通过enybody解释为什么我无法为该词典设定价值,并且enybody能告诉我其他任何方式吗?

提前致谢.

San*_*eep 13

Swift字典没有像setValue这样的方法:forKey:.只有NSMutableDictionary有这样的方法.如果你想在swift中为key赋值,你应该使用下标.如果你想用swift词典做这件事,这是正确的方法.

var imageDict:[String: String] = [:]

imageDict["\(tagValue)"] = NSStringFromCGRect(frame)
Run Code Online (Sandbox Code Playgroud)

或者,如果您希望使用NSMutableDictionary,它看起来像这样,

var imageDict = NSMutableDictionary()
imageDict.setObject(NSStringFromCGRect(frame), forKey: "\(tagValue)")
Run Code Online (Sandbox Code Playgroud)