我正在运行一段代码,在不同语言设置的iPhone上返回nil
。一个示例代码如下:
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "MMM d, yyyy, h:mm a"
let thisDate = "Mar 4, 2017, 7:50 PM"
let foundationDate = dateFormatter.string(from: Foundation.Date())
print("As String - thisDate: \(thisDate), foundationDate: \(foundationDate)")
print("As Date - thisDate: \(dateFormatter.date(from: thisDate)), foundationDate: \(dateFormatter.date(from: foundationDate))")
如果我运行这个程序,我会得到:
As String - thisDate: Mar 4, 2017, 7:50 PM, foundationDate: okt. 22, 2017, 7:57 PM
As Date - thisDate: nil, foundationDate: Optional(2017-10-22 17:57:00 +0000)
有没有人可以展示如何覆盖客户端设置,以便thisDate
不会返回nil?