简体   繁体   English

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

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

I am converting my existing Objective-C Project to Swift. 我正在将现有的Objective-C项目转换为Swift。 I am converting a function where i am getting the above error. 我正在转换一个函数,我得到上述错误。 Please check the bellow codes. 请检查以下代码。

Objective-C 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];
    }
}

And here is the swift codes. 这是快捷的代码。

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

Can anyone please tell me what i am doing wring ? 谁能告诉我我在做什么呢?

allTouches() function doesn't return an Set of AnyObject. allTouches()函数不返回Set of AnyObject。 It returns a Set of UITouch Object. 它返回一组UITouch对象。 Please check the documentation. 请查看文档。 Here is the correct code. 这是正确的代码。

@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.

相关问题 类型“T”不符合协议“AnyObject” - Type 'T' does not conform to protocol 'AnyObject' 类型'AnyObject'不符合协议'NSFetchRequestResult' - Type 'AnyObject' does not conform to protocol 'NSFetchRequestResult' 类型[String:String]不符合协议“ AnyObject” - type [String: String] does not conform to protocol 'AnyObject' 输入&#39;[NSObject:AnyObject]!&#39; 不符合协议&#39;DictionaryLiteralConvertible&#39; - Type '[NSObject : AnyObject]!' does not conform to protocol 'DictionaryLiteralConvertible' 类型MCSessionState不符合协议“ AnyObject” - Type MCSessionState does not conform to protocol 'AnyObject' 类型“ AnyObject”不符合协议“ sequenceType” - Type 'AnyObject' does not conform protocol 'sequenceType' 类型“ AnyObject”不符合协议“ BooleanType” - Type 'AnyObject' does not conform to protocol 'BooleanType' 类型&#39;Int32&#39;不符合协议&#39;AnyObject&#39;Swift? - Type 'Int32' does not conform to protocol 'AnyObject' Swift? 类型“ AnyObject.Protocol”的值与预期的字典值类型“ AnyObject”不符 - Value of type “AnyObject.Protocol” does not conform to expected dictionary value type 'AnyObject' 输入anyObject? 不符合“ StringLiteralConvertible” - Type anyObject? does not conform to 'StringLiteralConvertible'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM