在Swift 2中的UITouch

3

我正在尝试将我的Swift 1.2代码转换为2.0版本,我有一个如下的函数:

override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
    let anyTouch =  (touches.first as? UITouch)!
}

但是我遇到了以下错误:
Downcast from 'UITouch?' to 'UITouch' only unwraps optionals; did you mean to use '!'?

我该怎么修复它?

让任何触摸 = touches.first as! UITouch - Paulw11
http://prntscr.com/8k3pzs - Okan
1
也许只需要 let anyTouch = touches.first - Paulw11
哦,它起作用了。谢谢你。如果你回答这个问题,我会标记它。但是我还有一个问题。你知道如何解决这些吗?http://prntscr.com/8k3tjh - Okan
实际上,您可能想要 let anyTouch = touches.first!,您仍然需要解包可选项,但是您不需要向下转换,因为该类型已被指定为 UITouch。 - Paulw11
显示剩余5条评论
1个回答

6

在函数签名中,集合的内容类型被指定为UITouch,因此您无需进行向下转换。您只需要解包可选项即可。

override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
    let anyTouch =  touches.first
}

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