用另一个字典

时间:2017-09-27 11:05:19

标签: ios arrays swift dictionary

我正在尝试更新字典数组中的值,但未能这样做。 我就是这样做的:

    var latestServerData = [[String:AnyObject]]() // Global variable

in someFunction{
    print(latestServerData) 
    var dic1 = [String:AnyObject]()
    dic1 =latestServerData[4]
    dic1["isVisited"] = true as AnyObject
    print(dic1)
   latestServerData[4] = dic1
    print(latestServerData)
}

print(dic1)我有正确的数据,但它没有被latestServerData数组替换。我做错了什么,正确的方法是什么。任何解释正确的方法是值得赞赏的。 谢谢!

1 个答案:

答案 0 :(得分:1)

对我来说,你的代码工作正常

var latestServerData = [
    ["name": "Amit", "age": "27", "rollno": 12,"isVisited":true],
    ["name": "Ananad", "age": "26", "rollno": 10,"isVisited":true],
    ["name": "Kashyap", "age": "25", "rollno": 11,"isVisited":true],
    ["name": "Raj", "age": "25", "rollno": 07,"isVisited":true],
    ["name": "Akshya", "age": "28", "rollno": 13,"isVisited":true]
] as [[String:AnyObject]]

这对我有用

var dic1 = [String:AnyObject]()
dic1 = latestServerData[4]
dic1["isVisited"] = false as AnyObject
latestServerData[4] = dic1

这也是可行的解决方案

latestServerData[0]["isVisited"] = false as AnyObject

print(latestServerData)
  

[“name”:Amit,“age”:27,“rollno”:12,“isVisited”:0],[“name”:Ananad,“age”:26,“rollno”:10,“isVisited “:1],[”name“:Kashyap,”age“:25,”rollno“:11,”isVisited“:1],[”name“:Raj,”age“:25,”rollno“:7, “isVisited”:1],[“name”:Akshya,“age”:28,“rollno”:13,“isVisited”:0]]

观察数组中的First和Last对象。 isVisited“:0