类型'AnyObject'不符合协议'Hashable'

时间:2016-04-28 21:06:50

标签: ios swift uitableview swift2

我正在将现有的Objective-C项目转换为Swift。我正在转换一个函数,我得到上述错误。请检查以下代码。

目标-C

- (IBAction)accessoryButtonTapped:(id)sender event:(id)event
{
    NSSet *touches = [event allTouches];
    UITouch *touch = [touches anyObject];
    CGPoint currentTouchPosition = [touch locationInView:self.tableView];
    NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint: currentTouchPosition];
    if (indexPath != nil) {
        [self tableView: self.tableView accessoryButtonTappedForRowWithIndexPath: indexPath];
    }
}

这是快捷的代码。

@IBAction func accessoryButtonTapped(sender: AnyObject, event: AnyObject) {
    let touches: Set<AnyObject> = event.allTouches()! // I am getting the ERROR here
    let touch: UITouch = touches.first!
    let currentTouchPosition: CGPoint = touch.locationInView(self.tableView)
    let indexPath: NSIndexPath = self.tableView.indexPathForRowAtPoint(currentTouchPosition)!

    self.tableView(self.tableView, accessoryButtonTappedForRowWithIndexPath: indexPath)
}

任何人都可以告诉我我在做什么吗?

1 个答案:

答案 0 :(得分:0)

allTouches()函数不返回Set of AnyObject。它返回一组UITouch对象。请查看文档。这是正确的代码。

@IBAction func accessoryButtonTapped(sender: AnyObject, event: AnyObject) {
    let touches: Set<UITouch> = event.allTouches()!
    let touch: UITouch = touches.first!
    let currentTouchPosition: CGPoint = touch.locationInView(self.tableView)
    let indexPath: NSIndexPath = self.tableView.indexPathForRowAtPoint(currentTouchPosition)!

    self.tableView(self.tableView, accessoryButtonTappedForRowWithIndexPath: indexPath)
}