我正在尝试更改数组中字典的值.我在PlayGround中制作了一个小型原型:
var arr = [NSDictionary]() arr.append(["name":"blue","view":"
但是发生了错误,我无法解决:
有人能帮帮我吗?
因为您创建了字典NSDictionary
- 一旦设置了这些值就无法更改.但是你仍然想要改变它们setValue()
,这就是为什么你有错误.修复很简单,将其更改为NSMutableDictionary
.但是.当你有Swift API时,你不应该使用Objective-C API.这就是你应该使用Swift的原因Dictionary
.怎么样?例如
var arr = [[String:String]]() arr.append(["name":"blue","view":"","visible":"true","locked":"false"]) arr.append(["name":"yellow","view":" ","visible":"true","locked":"false"]) arr.append(["name":"green","view":" ","visible":"false","locked":"true"]) //test remove arr.removeAtIndex(2) arr.count //test edit var nameChange = arr[1] nameChange["name"] = "black"