Swift中的块显示错误"在调用"中缺少参数#2的参数。

时间:2015-02-21 17:37:11

标签: objective-c swift objective-c-blocks

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

Objective-C Sample

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

错误截图

Missing argument for parameter #2 in call

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

1 个答案:

答案 0 :(得分:7)

actionSheet.buttonPressedBlock属性。您正在尝试设置它。那你的等号在哪里?这就是你在Swift中设置的方法:

 myThing.myProperty = myValue

您尝试将此属性设置为块(函数)的事实不会改变任何内容。所以:

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