Crash parsing date strings but only on some iOS devices

47 Views Asked by At

Most of the iPhone devices and actual users that I have are working fine. But when I check the crash log in Xcode, I get an error that string_date is nil on only some devices.

Code_1 is a code that is modified because code_2 is said to be nil.

code_1:

let timeZone = TimeZone(identifier: "Asia/Seoul")
let dateComponents = DateComponents(timeZone: timeZone, year: 2000, month: 01, day: 01,hour: 00,minute: 00,second: 00)
let Formatter = DateFormatter()
Formatter.timeZone = TimeZone(identifier: "Asia/Seoul")
Formatter.dateFormat = "yyyyMMdd HHmmss"
let Date_1 = Calendar.current.date(from: dateComponents)
let string_date = Formatter.string(from:Date_1!)
let final_Date = Formatter.date(from: string_date) // <- Swift runtime failure: Unexpectedly found nil while unwrapping an Optional value

code_2:

let date_string: String = "20000101 000000"
let Formatter = DateFormatter()
Formatter.dateFormat = "yyyyMMdd HHmmss"
//Formatter.timeZone = NSTimeZone(name: "KST") as TimeZone?
Formatter.locale = NSLocale(localeIdentifier: "ko_KR") as Locale

let stringDate = Formatter.date(from: date_string) //<- Swift runtime failure: Unexpectedly found nil while unwrapping an Optional value

Code that sets the date to 2000-01-01/00:00:00:00 so that a certain result value is obtained on all iPhone phones.

0

There are 0 best solutions below