[英]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.