我有以下的代碼片段,我有一些設備崩潰:Swift:Downcast導致應用程序崩潰,爲什麼?
Crashed: com.apple.root.default-qos
EXC_BAD_ACCESS KERN_INVALID_ADDRESS at 0x00000000cbd
代碼:
var obj:AnyObject = command.arguments[0] as AnyObject!
var theData:AnyObject = obj["getContactImagesByEmails"] as AnyObject!
if let contactImagesByEmails:AnyObject = obj["emails"]{
if contactImagesByEmails is Array<String>{
/*line 176*/ let array:Array<String> =
contactImagesByEmails as Array<String> // CRASH happens here
results = WmSqliteImagesModel.getInstance.getImagesByEmailAsWmContactImage(array) as Dictionary<String,AnyObject>
}
}
完整的堆棧跟蹤
Thread : Crashed: com.apple.root.default-qos
0 libswiftCore.dylib 0x0000000100559794 swift_unknownRetain + 32
1 MyApp 0x000000010017c8a0 MyApp.Plugin.(getContactImagesByEmails (MyApp.Plugin) -> (ObjectiveC.CDVInvokedUrlCommand) ->()).(closure #1) (Plugin.swift:176)
2 MyApp 0x000000010017c8a0 MyApp.Plugin.(getContactImagesByEmails (MyApp.Plugin) -> (ObjectiveC.CDVInvokedUrlCommand) ->()).(closure #1) (Plugin.swift:176)
3 MyApp 0x00000001001790b0 partial apply forwarder for reabstraction thunk helper from @callee_owned() -> (@unowned()) to @callee_owned (@in()) -> (@out()) with unmangled suffix "125" (Plugin.swift:62)
4 MyApp 0x0000000100179120 partial apply forwarder for reabstraction thunk helper from @callee_owned (@in()) -> (@out()) to @callee_owned() -> (@unowned()) with unmangled suffix "128" (Plugin.swift:62)
5 libdispatch.dylib 0x00000001937e13ac _dispatch_call_block_and_release + 24
6 libdispatch.dylib 0x00000001937e136c _dispatch_client_callout + 16
7 libdispatch.dylib 0x00000001937ed40c _dispatch_root_queue_drain + 1152
8 libdispatch.dylib 0x00000001937ee75c _dispatch_worker_thread3 + 108
9 libsystem_pthread.dylib 0x00000001939bd2e4 _pthread_wqthread + 816
的Plugin.swift:176
點到:
let array:Array<String> = contactImagesByEmails as Array<String>
我想念什麼嗎?我認爲這個代碼應該是安全的。
如果contactImagesByEmails is Array<String>
返回true
,爲什麼contactImagesByEmails as Array<String>
失敗?
請幫幫忙,
[編輯]
command
有型的CDVInvokedUrlCommand
@interface CDVInvokedUrlCommand : NSObject {
NSString* _callbackId;
NSString* _className;
NSString* _methodName;
NSArray* _arguments;
}
你期望'var obj:AnyObject = command.arguments [0]作爲AnyObject!'做什麼?我可以看到它是如何編譯的,但我無法想象它如何做到有用。投射到「AnyObject!」是一個非常奇怪的沮喪。 – 2015-02-10 20:15:36
@RobNapier'obj [「getContactImagesByEmails」]'返回可選的,所以我解開它,並承諾'theData'不是'nil',否則它會在之前崩潰。我該如何寫這個? – snaggs 2015-02-10 20:17:46
你真正期望'command.arguments [0]'是什麼類型?你期望它是一個字典嗎?它是一個字典嗎?什麼類型是'命令?' – 2015-02-10 20:18:27