Swift:修改字典数组中的一个元素

时间:2017-04-24 22:43:14

标签: swift3 nsarray nsdictionary

我有一个像这样的字典数组:

{
Place = "somewhere";
Type = Any;
timeRegisted = "2017-04-24T16:15:00";
},

我正在修改从UTC到当地时间的时间。像这样:

let newJsonContent = jsonContent { (contents:Any) -> String in

            let dict:NSDictionary = contents as! NSDictionary
            let time:String = dict.object(forKey: "SchedDepTime") as! String
            print(time)

            let dateFormatter = DateFormatter()
            dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss"
            dateFormatter.timeZone = NSTimeZone(name: "UTC")! as TimeZone
            let date = dateFormatter.date(from: time)
            dateFormatter.dateFormat = "MM-dd-yyyy HH:mm"
            dateFormatter.timeZone = NSTimeZone.local
            let timeStamp = dateFormatter.string(from: date!)
            return time
        } 

将UTC转换为当地时间效果很好,但我对这些人提问。如何才能修改时间并保留字典的内容,而不仅仅是时间?

你们中的任何人都知道我该怎么办? 我非常感谢你的帮助。

1 个答案:

答案 0 :(得分:0)

首先返回字典而不是时间。

let dict:NSDictionary = contents as! NSDictionary
.
. //Do any modifications here
.
return dict

其次,您没有在上面的代码中修改任何内容。要修改你需要可变字典。

var dict:NSDictionary = contents as! NSDictionary
相关问题