My problem is try convert string to date and crash.My app is on App Store, and running on my device, but it crashes on some devices.I looked crashes log and see it,is getting crash when string to date.I don't understand why my device is working or some devices is working well but others is crash.
func toDate() -> Date {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss+03:00" //Your date format
dateFormatter.timeZone = TimeZone(abbreviation: "GMT+3:00") //Current time zone
//according to date format your date string
guard let date = dateFormatter.date(from: self) else {
fatalError()
}
return date
}
Two solutions:
en_US_POSIX
locale for a fixed format.Use ISO8601DateFormatter
(iOS 8+), the benefit is no locale, no date format, no time zone.
func toDate() -> Date? {
let dateFormatter = ISO8601DateFormatter()
return dateFormatter.date(from: self)
}
In any case return an optional, fatalError()
in production environment causes pretty bad user experience.