【问题标题】:Converting syntax from Objective C to Swift ^()将语法从 Objective C 转换为 Swift ^()
【发布时间】:2016-01-24 11:19:42
【问题描述】:

我有一个 Swift 项目,我在其中使用了一个 Objective C 自定义手势识别器。

到目前为止,我已经成功地转换了一切。不太清楚如何转换 return ^(UITableView *tableView, NSIndexPath *indexPath) { }; 我试过的一切都没有编译。

目标 C 代码:

 typedef void(^DRCellSlideActionBlock)(UITableView *tableView, NSIndexPath *indexPath);
    typedef void(^DRCellSlideActionStateBlock)(DRCellSlideAction *action, BOOL active);




  -(void)doSomething{
            squareAction.didTriggerBlock = [self pushTriggerBlock];
    }

- (DRCellSlideActionBlock)pushTriggerBlock {

    return ^(UITableView *tableView, NSIndexPath *indexPath) {
           NSLog(@"Do Something");
        }]];

        [self presentViewController:alertController animated:YES completion:nil];
    };
}

我尝试过的:

错误:连续语句必须分开......

   return (tableView: UITableView, indexPath: NSIndexPath) in {
}

也试过这个作为 (UIView.animateWithDuration) 的完成:

错误:连续语句必须分开......

return () -> (tableView: UITableView, indexPath: NSIndexPath) in {

顺便说一句,我对 Swift 有点陌生,所以请原谅我的代码

【问题讨论】:

    标签: ios objective-c swift


    【解决方案1】:

    为什么不直接在官方 Swift 指南中查找闭包的语法?你完全被闭包的样子误导了。

    return { (tableView: UITableView, indexPath: NSIndexPath) in
         // closure body here
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-08
      相关资源
      最近更新 更多