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 中有一条错误消息说
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/