简体   繁体   English

更改 SWIFT 中字典数组中特定键的值

[英]Change value of a particular key in array of dictionaries in SWIFT

Am new to swift and am using an array of multiple dictionaries.我是 swift 的新手,并且正在使用多个字典数组。 So i want to change the value of a particular key.所以我想更改特定键的值。 Please guide me how to achieve it:请指导我如何实现它:

Values In Array:- [ { "Start":"2:00 AM", "End":"2:00 AM" }, { "Start":"3:00 AM", "End":"3:00 AM" } ]数组中的值:- [ { "Start":"2:00 AM", "End":"2:00 AM" }, { "Start":"3:00 AM", "End":"3:00是” } ]

To Do:- I want to change the value of key Start at index 1待办事项:- 我想在索引 1 处更改键Start的值

Code:-代码:-

var aEntry = NSMutableArray.init()

for (index, type) in self.aEntry.enumerated(){
 print("this is index value \(index)")
 print("this is type value \(type)")

 // Value of checkCurrentClickOfStart = 1
                                
    if index == Int(checkCurrentClickOfStart) {
        let (item, newValue) = (index, "\(strHour)")
        if 0 ..< self.aEntry.count ~= index {
                          
            (self.aEntry[index] as? NSMutableDictionary)?["Start"] = newValue
        }
    }
                
}

First of all Use Array instead of NSMutableArray See the example code below首先使用 Array 而不是 NSMutableArray 请参见下面的示例代码

 var yourArray = [["Start":"2:00 AM", "End":"2:00 AM"], ["Start":"3:00 AM", "End":"3:00 AM"]]
    
// Change the value of a key "Start" at Index 1
 yourArray[1].updateValue("2:50 AM", forKey: "Start")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM