我正在使用MCSwipeTableViewCell框架来实现可滑动的tableviewcell。在cellForRowAtIndexPath
函数中,其中一个完成块看起来像这样:
[cell setSwipeGestureWithView:checkView color:greenColor mode:MCSwipeTableViewCellModeSwitch state:MCSwipeTableViewCellState1 completionBlock:^(MCSwipeTableViewCell *cell, MCSwipeTableViewCellState state, MCSwipeTableViewCellMode mode) {
// run some function call
}];
我使用了一个桥接头文件将该框架导入到我的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闭包中访问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");
}
-[__NSArrayM runSomeFunction]: unrecognized selector sent to instance
这个错误提示告诉你,你可能认为自己在访问self
,但实际上是在访问一个NS(Mutable)Array
。也许是内存问题? - Jack Lawrence