Swift3 Swift:修改字典数组中的一个元素
我有一系列这样的字典:Swift3 Swift:修改字典数组中的一个元素,swift3,nsarray,nsdictionary,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
{
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到当地时间的转换非常有效,但我想问大家一个问题。如何修改时间,保留字典的内容而不仅仅是时间
你们谁知道我该怎么做?
我真的很感激你的帮助。首先,把字典换成时间
let dict:NSDictionary = contents as! NSDictionary
.
. //Do any modifications here
.
return dict
其次,您没有修改上面代码中的任何内容。要修改,您需要可变字典
var dict:NSDictionary = contents as! NSDictionary
不相关,但不要使用
NSDictionary
或NSTimeZone
。使用Swift字典和时区。