Swift iOS:在尝试从AnyObject中按键获取值时的EXC_BAD_ACCESS

时间:2015-03-27 23:03:53

标签: ios swift

我有默认Dictionary

 var activeLanguage:Dictionary<String,AnyObject> = [
                                                    "name":"English",
                                                    "tag":"en",
                                                    "change_by_user":false,
                                                    "css_path":""
                                                    ]

从应用程序用户获得新配置,我的目标是验证字段并将新值存储到activeLanguage(a.e替换为新值)。如果某个键不存在,我会设置默认值。没什么复杂的。

这是我的实施:

 func build(obj:AnyObject){

        if let tempDict:Dictionary<String,AnyObject> = obj["activeLanguage"] as? Dictionary<String,AnyObject>{

            self.activeLanguage["name"] = tempDict["name"] as? String ?? activeLanguage["name"] // <-- CRASH here
            self.activeLanguage["tag"] = tempDict["tag"] as? String ?? activeLanguage["tag"]
            self.activeLanguage["change_by_user"] = tempDict["change_by_user"] as? Bool ?? false
            self.activeLanguage["css_path"] = tempDict["css_path"] as? String ?? activeLanguage["css_path"]

        }
}

我在几个客户端上测试过但在一个应用程序上崩溃了:

self.activeLanguage["name"] = tempDict["name"] as? String ?? activeLanguage["name"]

完整堆栈跟踪:

EXC_BAD_ACCESS KERN_INVALID_ADDRESS位于0x00000001ae36bec8

Thread : Crashed: com.apple.root.default-qos
0  libswiftCore.dylib             0x00000001004a17d0 swift_unknownRelease + 24
1  MyApp                       0x00000001000a4654 specialization <Swift.String with Swift.String : Swift.Hashable, Swift.AnyObject> of Swift._VariantDictionaryStorage.ensureUniqueNativeStorage <A : Swift.Hashable, B>(inout Swift._VariantDictionaryStorage<A, B>)(Swift.Int) -> (reallocated : Swift.Bool, capacityChanged : Swift.Bool) (MyAppPlugin.swift)
2  MyApp                       0x00000001000a2800 specialization <Swift.String with Swift.String : Swift.Hashable, Swift.AnyObject> of Swift._VariantDictionaryStorage.nativeUpdateValue <A : Swift.Hashable, B>(inout Swift._VariantDictionaryStorage<A, B>)(B, forKey : A) -> Swift.Optional<B> (MyAppPlugin.swift)
3  MyApp                       0x00000001000a1fa4 specialization <Swift.String with Swift.String : Swift.Hashable, Swift.AnyObject> of Swift.Dictionary.subscript.setter (A) -> Swift.Optional<B> (MyAppPlugin.swift)
4  MyApp                       0x00000001001a93e8 MyApp.WmConfigModel.build (MyApp.WmConfigModel)(Swift.AnyObject) -> () (WmConfigModel.swift:84)
5  MyApp                       0x00000001001ced54 MyApp.WmSettingsMgr.setSettings (MyApp.WmSettingsMgr.Type)(Swift.AnyObject) -> () (WmSettingsMgr.swift:29)
6  MyApp                       0x000000010009cce4 MyApp.MyAppPlugin.(setSettings (MyApp.MyAppPlugin) -> (ObjectiveC.CDVInvokedUrlCommand) -> ()).(closure #1) (<imports>)
7  MyApp                       0x0000000100099aa0 partial apply forwarder for reabstraction thunk helper from @callee_owned () -> (@unowned ()) to @callee_owned (@in ()) -> (@out ()) with unmangled suffix "102" (MyAppPlugin.swift:57)
8  MyApp                       0x0000000100099b10 partial apply forwarder for reabstraction thunk helper from @callee_owned (@in ()) -> (@out ()) to @callee_owned () -> (@unowned ()) with unmangled suffix "105" (MyAppPlugin.swift:57)
9  libdispatch.dylib              0x00000001978213ac _dispatch_call_block_and_release + 24
10 libdispatch.dylib              0x000000019782136c _dispatch_client_callout + 16
11 libdispatch.dylib              0x000000019782d40c _dispatch_root_queue_drain + 1152
12 libdispatch.dylib              0x000000019782e75c _dispatch_worker_thread3 + 108
13 libsystem_pthread.dylib        0x00000001979fd2e4 _pthread_wqthread + 816

(WmConfigModel.swift:84)指向:

self.activeLanguage["name"] = tempDict["name"] as? String ?? activeLanguage["name"]

0 个答案:

没有答案