gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-10-31 08:05:31 27 4
gpt4 key购买 nike

我将 MCSwipeTableViewCell 框架用于可滑动的 tableviewcell。 cellForRowAtIndexPath 函数中的一个完成 block 如下所示:

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

我使用 Bridging-Header 文件将框架导入到我的 Swift 项目中,并试图在 Swift 中使用相同的完成 block 。这是我的:

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'

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

有什么想法吗?我不应该尝试访问自己吗?

===更新===

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

这是正在运行的代码

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

最佳答案

你可以定义一个Capture List在像这样的闭包中使用 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()

关于ios - 在 Objective-C 框架中使用 Swift 闭包,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24619179/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com