启动时,某些设备上的iOS 13应用崩溃。该应用程序正在生产中,我已经在Xcode Organizer Crashs中看到了此问题。我不能在模拟器或任何物理设备上都重现此问题。
我想这与本地化有关。我使用“ 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崩溃中看到的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 ()
-------------------------------------------------------
答案 0 :(得分:0)
我什么都没有,所以这里有些假设可能无法正确解决:
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)
}
做了什么?
languageCode
现在已经安全地展开,以避免强制转换,否则可能导致崩溃Language.someLanguage
返回的一种情况。synchonize()
个呼叫