只有在 iOS 13 上运行时才会出现 DateFormater 错误 [英] DateFormater error only when running on iOS 13
本文介绍了只有在 iOS 13 上运行时才会出现 DateFormater 错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个将字符串转换为日期格式的函数.此功能在 iOS 12 上按预期工作,但在 iOS 13 上运行.我收到此错误:
I have a function to convert string to date format. This function works as expected on iOS 12 but on iOS 13. I get this error:
线程 1:致命错误:在展开可选值时意外发现 nil"
"Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value"
这是我的代码:
func ConvertDateAndTimeFormat2() {
let timeDate = "2019-09-24 15:00:00 +0000"
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss +zzzz"
dateFormatter.timeZone = TimeZone(abbreviation: "GMT+3:00")
var dateObj:Date!
dateObj = dateFormatter.date(from: timeDate)
dateFormatter.dateFormat = "EEEE, MMM d, yyyy"
dateFormatter.timeZone = TimeZone(abbreviation: "GMT+3:00")
let timeFormatter = DateFormatter()
timeFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss +zzzz"
timeFormatter.timeZone = TimeZone(abbreviation: "GMT+3:00")
var timeObj:Date!
timeObj = timeFormatter.date(from: timeDate)
timeFormatter.dateFormat = "HH:mm"
timeFormatter.timeZone = TimeZone(abbreviation: "GMT+3:00")
let timef = "\(timeFormatter.string(from: timeObj!))"
let Date = "\(dateFormatter.string(from: dateObj!))"
}
推荐答案
请更改您的日期格式并使用yyyy-MM-dd HH:mm:ss Z"并尝试.
Please change your dateformat and use "yyyy-MM-dd HH:mm:ss Z" and try.
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss Z"
更多详情请访问https://nsdateformatter.com/
这篇关于只有在 iOS 13 上运行时才会出现 DateFormater 错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文