Change value of a particular key in array of dictionaries in SWIFT

834 Views Asked by At

Am new to swift and am using an array of multiple dictionaries. 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" } ]

To Do:- I want to change the value of key Start at index 1

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
        }
    }
                
}
1

There are 1 best solutions below

0
waseemwk On

First of all Use Array instead of NSMutableArray See the example code below

 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")