【问题标题】:Change value of a particular key in array of dictionaries in SWIFT在 SWIFT 中更改字典数组中特定键的值
【发布时间】:2021-01-15 05:56:37
【问题描述】:

我是 swift 新手,并且正在使用多个字典的数组。所以我想改变一个特定键的值。请指导我如何实现它:

数组中的值:- [ { "开始":"凌晨 2:00", “结束”:“凌晨 2:00” }, { "开始":"凌晨 3:00", “结束”:“凌晨 3:00” } ]

待办事项:- 我想更改索引 1 处的键 Start 的值

代码:-

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

【问题讨论】:

  • 你可以直接像aEntry[Int(checkCurrentClickOfStart)]["Start"] = "(strHour)"那样使用
  • @Anbu.Karthik 它说“'Any' 类型的值没有下标”
  • @hemant-garg: ` if 0 ..
  • 停止使用 NS 类,改用 swift 类型
  • 您可以在Swift Programming Language 书中阅读有关 swift 集合类型的信息

标签: ios arrays swift dictionary nsmutablearray


【解决方案1】:

首先使用 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")

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-11-10
    • 2022-11-24
    • 1970-01-01
    • 2014-12-10
    • 1970-01-01
    • 2020-11-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多