Swift 中的块显示错误"Missing argument for parameter #2 in call"



我现在在我的项目中使用Jonas Gessner的JGActionSheet和Swift,样本是由Objective-C编写的,当我试图将块转换为Swift时,Xcode显示错误"调用中参数#2缺少参数",这是我编写的代码和屏幕截图:

Objective-C示例

JGActionSheet *sheet = [JGActionSheet actionSheetWithSections:sections];
[sheet setButtonPressedBlock:^(JGActionSheet *sheet, NSIndexPath *indexPath) 
{
    [sheet dismissAnimated:YES];
}];

我用Swift写的代码

let actionSheet = JGActionSheet(sections: sections)
actionSheet.buttonPressedBlock {
    (sheet: JGActionSheet!, indexPath: NSIndexPath!) in
    actionSheet.dismissAnimated(true)
}

错误屏幕截图

调用中缺少参数#2的参数

所以请帮我解决这个问题,非常感谢!

actionSheet.buttonPressedBlock属性。你正试图设置。那么你的等号在哪里?这就是你在Swift中设置的方式:

 myThing.myProperty = myValue

您正试图将此属性设置为块(函数),这一事实不会改变任何内容。因此:

let actionSheet = JGActionSheet(sections: sections)
actionSheet.buttonPressedBlock = {
    (sheet: JGActionSheet!, indexPath: NSIndexPath!) in
    actionSheet.dismissAnimated(true)
}

最新更新