iOS14 和 MacOS 11 中的 ISO8601DateFormatter 是否损坏?

Is ISO8601DateFormatter broken in iOS14 and MacOS 11?

我正在 WWDC 之后试用最新的测试版。

ISO8601DateFormatter().date(from:"2017-12-18T11:30:26.000Z") 应该 return 一个日期。相反,它是 returning nil。这在 iOS 13.

中有效

我在Swift Playground 分解并测试了它。我正在使用 Xcode 12.0 版测试版 (12A6159)

let testString = "2017-12-18T11:30:26.000Z"
let testString2 = "2017-12-18"
let testFormatter = ISO8601DateFormatter()
let testDate1 = testFormatter.date(from: testString)
let testDate2 = testFormatter.date(from: testString2)

testDate1testDate2 应该是日期,它们是 nils。

testFormatter 部分功能正常。 returns 下面的行是预期的字符串形式的当前日期; “2020-06-27T18:53:39Z”。

let testDate3 = testFormatter.string(from: Date())

我是不是遗漏了什么,或者这只是一个早期的测试版错误?我已经向 Apple 提交了反馈,但我希望这里有人能给出答案。

您发布的代码在 iOS 13 中也不起作用。

尝试以下操作:

let testString = "2017-12-18T11:30:26.000Z"
let testString2 = "2017-12-18"
let testFormatter = ISO8601DateFormatter()
testFormatter.formatOptions = [
    .withFullDate,
    .withFullTime,
    .withFractionalSeconds,
]
let testDate1 = testFormatter.date(from: testString)
testFormatter.formatOptions = [.withFullDate]
let testDate2 = testFormatter.date(from: testString2)