类型“AnyObject”不符合协议“Hashable”。

3
我正在将我的现有 Objective-C 项目转换为 Swift。我正在转换一个函数,在该函数中出现了上述错误。请检查下面的代码。
Objective-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

allTouches()函数不返回任何对象的Set。它返回一个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)
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接