Update 15.4 crashes after online feedback. According to the screenshots from the feedback, they are all in the 12-hour time format.

After updating the latest version of XCode, it is found that the emulator default time is 12 hours, but there is no crash;

So I updated the test machine to version 15.4 and tested it on a 12-hour schedule. The code for finding the problem is as follows

DateFormat = "YYYY /MM/ DD HH: MM :ss" let dfmatter = DateFormatter() dfmatter. DateFormat = "YYYY /MM/ DD HH: MM :ss" "2020/02/02 17:22:38") let dateStamp:TimeInterval = date! .timeIntervalSince1970Copy the code

This time format conversion code feedback date nil, resulting in a crash;

Here, a code specification problem was first discovered, which could have been solved without forcing the system to crash. Because this code has also been used for 6 years, migrating from OC to Swift generally does not cause a crash, so I did not think much about using the strong solution directly, but unexpectedly fell into a pit in version 15.4.

And let’s see why that would cause it to be nil,

I run the current timestamp as “YYYY-MM-DD HH: MM: SS” on the real device and print the following

iOS15.4

  • On 12-hour schedule 2020/02/02 5:22:38 PM

  • In 24-hour format 2020/02/02 17:22:38

Other System Versions

  • 12-hour system 2020/02/02 17:22:38

  • In 24-hour format 2020/02/02 17:22:38

Prints out the parameters of the DateFormat object

DateFormat() was changed in iOS15.4.

Looking through the developer documentation, I found a similar situation in a technical Q&A

- On iOS, the user can override the default AM/PM setting with 24 hour time (via Settings > General > Date and Time > 24 hour Time), which causes NSDateFormatter to override the format string you set, which may cause your time resolution to fail.Copy the code

Attached to the developer documentation: developer.apple.com/library/arc…

Try adding local and it will resolve properly again.

But oddly enough, the printed dateformat local is “zh_CN”.

Finally, attach the code that can parse properly:

DateFormat = "YYYY /MM/ DD HH: MM :ss" dfmatter. Locale = locale (identifier: "zh_CN") let date = dfmatter.date(from: "2020/02/02 17:22:38") let dateStamp:TimeInterval = date! .timeIntervalSince1970Copy the code