ios - Swift:Downcast 导致应用程序崩溃,为什么?

标签 ios cordova swift

我在某些设备上崩溃的代码片段如下:

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;
}

最佳答案

我不知道这是否真的能解决问题,但在 2 个 if 和后面的 let 中有一点冗余。可以简单的写成:

if let contactImagesByEmails = obj["emails"] as? Array<String> {
    results = WmSqliteImagesModel.getInstance.getImagesByEmailAsWmContactImage(contactImagesByEmails) as Dictionary<String,AnyObject>
}

此外,在调用 getImagesByEmailAsWmContactImage 时结合可选绑定(bind)和可选向下转换会更安全

if let results = WmSqliteImagesModel.getInstance.getImagesByEmailAsWmContactImage(contactImagesByEmails) as? Dictionary<String,AnyObject> {
    ...
}

最后,您确定 getInstance 是属性而不是方法吗?不应该是:

if let results = WmSqliteImagesModel.getInstance().getImagesByEmailAsWmContactImage(contactImagesByEmails) as? Dictionary<String,AnyObject> {
    ...
}

关于ios - Swift:Downcast 导致应用程序崩溃,为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28440815/

相关文章:

javascript - 如何使用 AngularJS/Ionic 处理 Android 键盘应用程序上的 'done' 按钮?

cordova - ionic 3 : Execution failed for task ':app:processDebugResources'

ios - 如何在 Xcode 中执行数据驱动测试

swift - 为 MKClusterAnnotation 设置 MKMarkerAnnotationView 的 glyphText

ios - 无法将类型 'UIView' (0x112484eb0) 的值转换为 'SKView' (0x111646718)

ios - UITableView IBOutlet 在 ViewDidLoad 上崩溃,Apple Beta 审查小组不适合我

javascript - 无法在 Ionic 3 中链式 promise

ios - UISearchBar拼写检查

_updateInterfaceOrientationFromDeviceOrientation 上的 ios8 崩溃

IOS应用加密