gpt4 book ai didi

swift - 更改字典 swift 5 数组中的值

转载 作者:搜寻专家 更新时间:2023-11-01 06:52:24 25 4
gpt4 key购买 nike

我正在使用 xcode 10.2 和 swift 5

我需要更改“arrNotificationList”中“selected”key = false/true 的所有值

    // Create mutable array
var arrNotificationList = NSMutableArray()

// viewDidLoad method code
arrNotificationList.addObjects(from: [
["title":"Select All", "selected":true],
["title":"Match Reminder", "selected":false],
["title":"Wickets", "selected":false],
["title":"Half-Centure", "selected":false],
])

我已尝试使用以下代码,但原始数组“arrNotificationList”值未更改。

        arrNotificationList.forEach { value in
print("\(value)")
var dictNotification:[String:Any] = value as! [String : Any]
dictNotification["selected"] = sender.isOn // this is switch value which is selected by user on/off state
}

最佳答案

要更改数组元素,请使用 map函数而不是 forEach .然后在map函数中返回change改变的字典

var arrNotificationList = [[String:Any]]()

arrNotificationList = [["title":"Select All", "selected":true],
["title":"Match Reminder", "selected":false],
["title":"Wickets", "selected":false],
["title":"Half-Centure", "selected":false]]
arrNotificationList = arrNotificationList.map({
var dict = $0
dict["selected"] = sender.isOn
return dict
})
print(arrNotificationList)

关于swift - 更改字典 swift 5 数组中的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56271775/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com