2017-04-24 41 views
0

数组一个元素我有字典的一个这样的数组:斯威夫特:修改词典

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

我从UTC修改timeRegisted为本地时间。像这样:

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到本地时间的转换很好,但我的问题给家伙。如何修改时间并保留字典的内容而不仅仅是时间?

你们谁都知道我该怎么做? 我会非常感谢你的帮助。

+2

无关,但不使用'NSDictionary'或'NSTimeZone'。使用Swift词典和'TimeZone'。 – rmaddy

回答

0

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

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

其次你没有修改上面的代码中的任何东西。要修改你将需要可变的字典。

var dict:NSDictionary = contents as! NSDictionary