gpt4 book ai didi

iOS 13 用户默认值 : app crash on start on some devices

转载 作者:行者123 更新时间:2023-11-28 07:20:17 25 4
gpt4 key购买 nike

iOS 13 上的应用程序在某些设备上启动时崩溃。该应用程序正在生产中,我在 Xcode Organizer Crashes 中看到了这个问题。我无法在模拟器或任何物理设备上重现此问题。

我想这与本地化有关。我使用“AppleLanguages”键并将所选语言保存为数组。我将用户默认设置中的语言保存为:

static var language: Language {
get {

if UserDefaults.standard.bool(forKey: isLanguageSelectedKey) {
if let languageCode = UserDefaults.standard.array(forKey: appleLanguagesKey)?.first,
let language = Language(rawValue: languageCode as! String) {
return language
} else {

return Language.someLanguage
}
} else {
return Language.someLanguage
}

}

set {
guard language != newValue else {
return
}
UserDefaults.standard.removeObject(forKey: appleLanguagesKey)
UserDefaults.standard.synchronize()

UserDefaults.standard.set([newValue.rawValue], forKey: appleLanguagesKey)

UserDefaults.standard.synchronize()
}

我在 Xcode Organizer Crashes 中看到的 Stacktrace 是:

Last Exception Backtrace (0)
0 (null) in __exceptionPreprocess ()
-------------------------------------------------------
8 (null) in +[NSUserDefaults(NSUserDefaults) standardUserDefaults] ()
9 (null) in __45+[GMSx_GTMSessionFetcher fetcherUserDefaults]_block_invoke ()
10 (null) in _dispatch_client_callout ()
11 (null) in _dispatch_once_callout ()
12 (null) in +[GMSx_GTMSessionFetcher fetcherUserDefaults] ()
13 (null) in +[GMSx_GTMSessionFetcher fetchersForBackgroundSessions] ()
14 (null) in +[GMSx_GTMSessionFetcher load] ()
15 (null) in load_images ()
-------------------------------------------------------



Thread 1
0 (null) in __pthread_kill ()
-------------------------------------------------------
9 (null) in _dispatch_once_callout ()
10 (null) in +[GMSx_GTMSessionFetcher fetcherUserDefaults] ()
11 (null) in +[GMSx_GTMSessionFetcher fetchersForBackgroundSessions] ()
12 (null) in +[GMSx_GTMSessionFetcher load] ()
13 (null) in load_images ()
-------------------------------------------------------

最佳答案

我没有所有的东西,所以这里有一些假设可能没有得到妥善解决:

static var language: Language {
get {
if UserDefaults.standard.bool(forKey: isLanguageSelectedKey),
let languageCode = UserDefaults.standard.array(forKey: appleLanguagesKey)?.first as? string,
let language = Language(rawValue: languageCode) {
return language
}
// No language value found, so return someLanguage
return Language.someLanguage

}
set {
guard language != newValue else {
return
}
UserDefaults.standard.removeObject(forKey: appleLanguagesKey)
UserDefaults.standard.set([newValue.rawValue], forKey: appleLanguagesKey)
}

做了什么?

  1. languageCode 现在已安全解包以避免强制转换,这可能会导致崩溃
  2. 重写了 if 语句,使其更加简洁,并消除了导致 Language.someLanguage 返回的情况
  3. 我删除了你的 setter 中的 synchonize() 调用

关于iOS 13 用户默认值 : app crash on start on some devices,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58414197/

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