在Objective-C框架中使用Swift闭包

布莱恩·温赖希

我正在将MCSwipeTableViewCell框架用于可滑动的tableviewcell。cellForRowAtIndexPath函数内部的完成块之一如下所示:

[cell setSwipeGestureWithView:checkView color:greenColor mode:MCSwipeTableViewCellModeSwitch state:MCSwipeTableViewCellState1 completionBlock:^(MCSwipeTableViewCell *cell, MCSwipeTableViewCellState state, MCSwipeTableViewCellMode mode) {
      // run some function call
}];

我使用Bridging-Header文件将框架导入到我的Swift项目中,并尝试在Swift中使用相同的完成块。这就是我所拥有的:

cell.setSwipeGestureWithView(crossView, color: UIColor.colorFromRGB(RED), mode: MCSwipeTableViewCellMode.Switch, state:MCSwipeTableViewCellState.State1, completionBlock: { (cell: MCSwipeTableViewCell!, state: MCSwipeTableViewCellState!, mode: MCSwipeTableViewCellMode!) -> Void in
    self.runSomeFunction();
});

问题是,self.runSomeFunction()即使函数调用已实现,它也会在每次运行时崩溃错误是

无法识别的选择器

sent to instance 0x165c7390
2014-07-07 16:23:14.809 pong[3950:60b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayM runSomeFunction]: unrecognized selector sent to instance 0x165c7390'

我知道完成模块是有效的,因为我可以从中获取NSLog并显示某些内容,但是尝试访问self总是会导致崩溃。

有任何想法吗?我不应该尝试进入自我吗?

===更新===

我主要要弄清楚的是如何self在Swift闭包中进行访问它总是抛出错误的访问错误。

这是正在运行的代码

 func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell!
{
    var cell = tableView.dequeueReusableCellWithIdentifier("userCell") as MCSwipeTableViewCell!

    if !cell {
        cell = MCSwipeTableViewCell(style: UITableViewCellStyle.Value1, reuseIdentifier: "userCell")
     }
    cell.setSwipeGestureWithView(crossView, color: UIColor.colorFromRGB(RED), mode: MCSwipeTableViewCellMode.Switch, state:MCSwipeTableViewCellState.State1, completionBlock: { (cell: MCSwipeTableViewCell!, state: MCSwipeTableViewCellState!, mode: MCSwipeTableViewCellMode!) -> Void in
        self.runSomething();
    });
    return cell
}

 func runSomething()
{
    NSLog("hey there");
}
埃桑分娩

您可以定义一个捕获列表self在闭包内部使用,如下所示:

cell.setSwipeGestureWithView(crossView, color: UIColor.colorFromRGB(RED), mode: MCSwipeTableViewCellMode.Switch, state:MCSwipeTableViewCellState.State1) {
    [unowned self]
    cell, state, mode in
    self.runSomething()
}

目前[unowned self]有时可能会崩溃,因此暂时[weak self]无法使用,并且在您的封包内部展开self如下:self!.doSomething()

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

使用Swift枚举的Objective-C闭包

SWIFT闭包语法-从Objective C转换

在Xcode Swift项目中使用Objective C框架

将Objective-C块转换为Swift闭包

将Objective-C块转换为Swift闭包

通过`id`在Objective-C中调用Swift闭包

将回调/闭包从 Objective C 转换为 Swift

使用尾随闭包的Swift到Objective-C调用会调用错误的方法

在Objective-C中将闭包(Swift)转换为闭包(block?)

在Xamarin iOS项目中使用Swift框架内的Objective C框架

Swift中使用的Objective-C框架-方法错误的模棱两可使用

如何在框架的Objective-C文件中使用Swift类?

如何在没有桥接头的情况下在 Swift 中使用 Objective-C 框架?

如何在框架中使用Swift扩展Objective-C类?

我可以在Swift框架库中使用Objective-C类吗?

在Objective-C中使用Swift协议

在Objective-C中使用Swift类

在Swift中将闭包设置为变量,然后尝试从Objective C类调用

如何从 Objective-C 调用带有闭包的 Swift 异步方法

如何将Objective-C块转换为Swift闭包?

将Swift 2闭包转换为Objective-C块

iOS中块(Objective-C)和闭包(Swift)之间的区别

将Objective-C块变量转换为Swift闭包变量?

使用Swift和Objective-C的iOS框架,其中Objective-C使用Swift C

快速将Objective-C块转换为闭包

使用Swift构建Cocoapod并依赖Objective-C框架

使用Swift语言处理Objective-C框架时出错

在 Swift 中导入 Objective-c 框架?

在Swift Framework中添加Objective C框架