我正在尝试使用此代码将 String 转换为 NSDate
let dateString = data as! String
let formatter = DateFormatter()
formatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZ"
let date = formatter.date(from: dateString)
let Calendar = NSCalendar(calendarIdentifier: NSCalendar.Identifier.gregorian)
let unitFlags: NSCalendar.Unit = [.hour, .day, .month, .year]
let components = Calendar?.components(unitFlags, from: date!)
并且dateString
是"2017-04-03T18:50:34.3042751+04:30"
,但我得到fatal error: unexpectedly found nil while unwrapping an Optional value
了最后一行。我究竟做错了什么?