Conversion String to NSDate - Fatal error: Unexpectedly found nil while unwrapping an Optional value
Conversion String to NSDate - Fatal error: Unexpectedly found nil while unwrapping an Optional value
怎么了?
相同的代码适用于其他 类。当我将字符串转换为 nsdate 时,模拟器崩溃了。
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "EEE, dd/MM/yyyy (HH:mm)"
//ordino il file
var dataminima = dateFormatter.date(from: "lun, 01/01/2010 (01:01)")
var datamassima = dateFormatter.date(from: "lun, 01/01/2010 (01:01)")
var indice = 0
var indiceminimo = 1
var indice1 = 0
for data in arraycomplessivo {
let validData = data as! NSDictionary
let dataestratta = validData["data"]
print(dataestratta as! String)
//converto da stringa a NSDate - that's the error
let dataconvertita = dateFormatter.date(from:dataestratta as! String)!
//print(dataminima!)
print(dataconvertita)
// dataestratta 是:gio,10/06/2021 (12:00)
error in console
模拟器中的语言环境不一定与您的设备或 Mac 中的语言环境相同。
由于字符串日期格式显然是意大利语,因此添加固定语言环境
dateFormatter.locale = Locale(identifier: "it_IT")
dateFormatter.dateFormat = "EEE, dd/MM/yyyy (HH:mm)"
怎么了? 相同的代码适用于其他 类。当我将字符串转换为 nsdate 时,模拟器崩溃了。
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "EEE, dd/MM/yyyy (HH:mm)"
//ordino il file
var dataminima = dateFormatter.date(from: "lun, 01/01/2010 (01:01)")
var datamassima = dateFormatter.date(from: "lun, 01/01/2010 (01:01)")
var indice = 0
var indiceminimo = 1
var indice1 = 0
for data in arraycomplessivo {
let validData = data as! NSDictionary
let dataestratta = validData["data"]
print(dataestratta as! String)
//converto da stringa a NSDate - that's the error
let dataconvertita = dateFormatter.date(from:dataestratta as! String)!
//print(dataminima!)
print(dataconvertita)
// dataestratta 是:gio,10/06/2021 (12:00)
error in console
模拟器中的语言环境不一定与您的设备或 Mac 中的语言环境相同。
由于字符串日期格式显然是意大利语,因此添加固定语言环境
dateFormatter.locale = Locale(identifier: "it_IT")
dateFormatter.dateFormat = "EEE, dd/MM/yyyy (HH:mm)"