Swift中的块显示错误“调用中参数#2的缺失参数”

时间:2021-02-10 09:40:26

I'm now using Jonas Gessner's JGActionSheet with Swift in my project, and the sample was written by Objective-C, when I tried to convert the block to Swift, Xcode shows the error "Missing argument for parameter #2 in call", here is the code I written and the screenshot:

我现在在我的项目中使用Jonas Gessner和Swift的JGActionSheet,这个示例是Objective-C编写的,当我试图将块转换为Swift时,Xcode显示了错误的“调用中参数#2的缺失参数”,这是我编写的代码,截图如下:

Objective-C Sample

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

Code I written in Swift

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

Error screenshot

Missing argument for parameter #2 in call

在调用中丢失参数#2的参数

So please help me to figure this out and thanks very much!

所以请帮我弄明白,非常感谢!

1 个解决方案

#1


7  

actionSheet.buttonPressedBlock is a property. You are trying to set it. So where's your equals sign? This is how you set things in Swift:

actionSheet。buttonPressedBlock是一个属性。你在尝试设置它。等号在哪里?这就是你如何设定事物迅速:

 myThing.myProperty = myValue

The fact that you are trying to set this property to a block (a function) changes nothing. So:

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

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

#1


7  

actionSheet.buttonPressedBlock is a property. You are trying to set it. So where's your equals sign? This is how you set things in Swift:

actionSheet。buttonPressedBlock是一个属性。你在尝试设置它。等号在哪里?这就是你如何设定事物迅速:

 myThing.myProperty = myValue

The fact that you are trying to set this property to a block (a function) changes nothing. So:

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

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