gpt4 book ai didi

ios - NSDateFormatter dateFromString崩溃

转载 作者:行者123 更新时间:2023-12-01 18:06:39 26 4
gpt4 key购买 nike

Crashlytics服务报告一些崩溃(大约30个崩溃/ 18个用户进行1000个 session )

这是我的代码:

var brutDate: String = ""
brutDate <- map["send_date"]

let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"

date = dateFormatter.dateFromString(brutDate)! // here is the crash (this is line 42)

崩溃日志是:
Crashed: NSOperationQueue 0x170422a80 :: NSOperation 0x170257190 (QOS: UTILITY)
0 AppName 0x100085f84 specialized SNotification.mapping(Map) -> () (SNotification.swift:42)
1 AppName 0x100084ff4 SNotification.mapping(Map) -> () (SNotification.swift:29)
2 AppName 0x100085a78 protocol witness for Mappable.mapping(Map) -> () in conformance SNotification (SNotification.swift:29)

在这种情况下,brutDate的值为 2017-01-31 20:02:08
我无法使应用程序崩溃我的手机...

编辑:我在法国,应用程序已部署到加拿大,是否存在 Locale问题?

最佳答案

可能是语言环境问题或设备问题。如果设备将24小时设置为关闭,则格式化程序将使用该时间,重写您提供的格式字符串。您可以通过将时间格式设置为12小时在本地重现此内容。

为避免这种情况,请对日期格式程序使用en_US_POSIX语言环境,这将使其使用您提供的未经修改的格式:

dateFormatter.locale = NSLocale(localeIdentifier: "en_US_POSIX")

这是 here解释的。

无论如何,最好还是不要使用 !以及日志记录和错误消息,或者如果无法从字符串中恢复默认值,请提供合理的默认值。

关于ios - NSDateFormatter dateFromString崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41977211/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com