ios - Swift 3 破坏了我的 Objective C 和 Swift 处理程序/闭包兼容性

标签 ios swift closures swift3

SIAlertView 是我在项目中使用的外部 Objective C 库

在 Swift 2.2 中下面的这一行可以正常编译

class func showAlert(title title : String, message: String, confirmHandler: (SIAlertView!) -> Void) {

    ...
    alertView.addButtonWithTitle(OK_TITLE, type: SIAlertViewButtonType.Default, handler: confirmHandler);
    alertView.show();
}

但现在在 Swift 3 中有一条错误消息说

enter image description here

Cannot convert value of type 'class func showAlert(title title : String, message: String, confirmHandler: (SIAlertView!) -> Void)' to expected argument type 'SIAlertViewHandler!'

我从 Objective C 类中发现

SIAlertView.h

@class SIAlertView;
typedef void(^SIAlertViewHandler)(SIAlertView *alertView);

SIAlertView.m

- (void)addButtonWithTitle:(NSString *)title type:(SIAlertViewButtonType)type handler:(SIAlertViewHandler)handler
{
    SIAlertItem *item = [[SIAlertItem alloc] init];
    item.title = title;
    item.type = type;
    item.action = handler;
    [self.items addObject:item];
}

有人能解释一下吗?因为它以前在 Swift 2.2 中运行良好

最佳答案

改变这个

class func showAlert(title title : String, message: String, confirmHandler: (SIAlertView!) -> Void)

对此

class func showAlert(title title : String, message: String, confirmHandler: (SIAlertView?) -> Void)

在 Objective C 中,此参数可以为 nil,因此我们必须将其声明为可选。

关于ios - Swift 3 破坏了我的 Objective C 和 Swift 处理程序/闭包兼容性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39630199/

相关文章:

ios - 从我的共享扩展 ShareViewController 显示一个 UIAlertController

ios - 如何在swift中创建具有多个参数的选择器

c# - 在闭包中捕获变量时,添加大括号如何更改生成的 IL 代码?

javascript - 如何使用闭包在循环中使用 then()

ios - NSDate 将时区更改为 GMT +02.00

ios - 尝试测试应用内购买时不允许创建 iTunes 帐户

ios - RMStore离线收货验证

objective-c - 如何只选择 iCarousel 的中心图像?

ios - 单击 SpriteKit 叠加标签时如何在 SceneKit 中切换场景?

javascript - 奇怪的 JavaScript 行为