繁体   English   中英

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

[英]Type 'AnyObject' does not conform to protocol 'Hashable'

我正在将现有的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)
}

谁能告诉我我在做什么呢?

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM