【发布时间】:2015-08-06 05:30:25
【问题描述】:
我在模拟器和 iPhone 6+ 中使用 NSDateFormatter 快速运行时遇到了一个奇怪的问题。我正在尝试将我在 JSON 中获得的特定格式的字符串转换为具有以下扩展名的 NSDate:
extension String {
var asStringDate: NSDate? {
get {
let dateFormatter = NSDateFormatter()
let offsetTime = NSTimeInterval(NSTimeZone.localTimeZone().secondsFromGMT) //support local timezone
dateFormatter.locale = NSLocale.currentLocale()
dateFormatter.dateFormat = "EEE, d MMM yyyy H:mm"
if let date = dateFormatter.dateFromString(self) {
let finalDate = date.dateByAddingTimeInterval(offsetTime) //support local timezone
// if Constants.debug { println("\(self) -> \(finalDate)") } //For debugging purposes
return finalDate
}
println("Error while formating string:'\(self)' to date in String.asStringDate") //For debugging purposes
return nil
}
}
出于某种原因,在模拟器上运行正常,但在我的 iPhone 上从 Xcode 运行时,我从
得到 nildateFormatter.dateFromString(self)
我得到的字符串示例:“Fri, 19 Jun 2015 15:52” 任何想法为什么它在 iPhone 中失败?
【问题讨论】:
标签: ios swift nsdateformatter