swift - 为什么我不能对字典使用 SetValue?

标签 swift xcode6

大家好,我是 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/

相关文章:

ios - 在 Xcode 中切换运行脚本构建阶段

c - 错误_SDL_Main SDL Mac Xcode 6 C 语言

ios - 无法在 Swift AppDelegate 文件中访问 Google Map Framework

ios - 无法将类型 'Dictionary<String?, String?>.Keys' 的值分配给类型 'String'

ios - 索引(其中 :) method in swift is producing the wrong index

ios - UITableViewCell 更新模型的正确 MVC 方法?

ios - 在Xcode项目之间共享类

ios - 数组数减去数组数

ios - 如何使用swift 4.1获取url参数的值

xcode - Xcode插件停止工作