我正在尝试调试第3方库中遇到的问题,并且遇到了麻烦。问题是我正在Swift中调用库,并将其传递给回调函数。该库然后执行Obj-C代码,最终以NULL
调用回调,而该混乱以EXC_BAD_ACCESS (code=1, address=0x0)
崩溃
快捷代码:
return AWSMobileClient.sharedInstance().interceptApplication(app, didFinishLaunchingWithOptions: opts) { (res: Any?, err: Error?) in
guard err == nil else {
Log.e("Error registering with AWS: \(err!.localizedDescription)")
return
}
Log.i("No error registering with AWS")
}
Obj-C代码(简体):
- (BOOL)interceptApplication:(UIApplication *)application
didFinishLaunchingWithOptions:(nullable NSDictionary *)launchOptions
resumeSessionWithCompletionHandler:(void (^)(id result, NSError *error))completionHandler {
completionHandler(NULL, NULL); // Crashes here if 1st argument is NULL
completionHandler(@"hello", NULL); // Doesn't crash
return YES;
}
如您所见,我已经正确地将Swift回调标记为类型Any?
,因此它应该接受NULL/nil
没问题。
如果任何人都可以解释为什么会发生这种情况以及如何解决它,那将是很棒的事情。