Jam*_*mes 2 nsdictionary nsarray swift swift2
我正在尝试更改数组中字典的值.我在PlayGround中制作了一个小型原型:
var arr = [NSDictionary]()
arr.append(["name":"blue","view":"<object id=\"6787\">","visible":"true","locked":"false"])
arr.append(["name":"yellow","view":"<object id=\"345\">","visible":"true","locked":"false"])
arr.append(["name":"green","view":"<object id=\"123\">","visible":"false","locked":"true"])
//test remove
arr.removeAtIndex(2)
arr.count
//test edit
let nameChange = arr[1]
nameChange.setValue("black", forKey: "name")
arr[1]
Run Code Online (Sandbox Code Playgroud)
有人能帮帮我吗?
因为您创建了字典NSDictionary- 一旦设置了这些值就无法更改.但是你仍然想要改变它们setValue(),这就是为什么你有错误.修复很简单,将其更改为NSMutableDictionary.但是.当你有Swift API时,你不应该使用Objective-C API.这就是你应该使用Swift的原因Dictionary.怎么样?例如
var arr = [[String:String]]()
arr.append(["name":"blue","view":"<object id=\"6787\">","visible":"true","locked":"false"])
arr.append(["name":"yellow","view":"<object id=\"345\">","visible":"true","locked":"false"])
arr.append(["name":"green","view":"<object id=\"123\">","visible":"false","locked":"true"])
//test remove
arr.removeAtIndex(2)
arr.count
//test edit
var nameChange = arr[1]
nameChange["name"] = "black"
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1450 次 |
| 最近记录: |