本文介绍了event.touchesForView().AnyObject() 在 Xcode 6.3 中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这在以前非常有效:
func doSomethingOnDrag(sender: UIButton, event: UIEvent) {
let touch = event.touchesForView(sender).AnyObject() as UITouch
let location = touch.locationInView(sender)
}
但在 Xcode 6.3 中,我现在收到错误:
But in Xcode 6.3, I now get the error:
不能不带参数调用AnyObject"
我该如何解决这个问题?
How do I fix this?
推荐答案
在 1.2 中,touchesForView
现在返回原生 Swift Set
而不是 NSSet
,而 Set
没有 anyObject()
方法.
In 1.2, touchesForView
now returns a native Swift Set
rather than an NSSet
, and Set
doesn't have an anyObject()
method.
它确实有一个 first
方法,这很相似.另请注意,您将无法再使用 as?
,您必须使用 as?
进行转换并处理零可能性,这是一个方法:
It does have a first
method, which is much the same thing. Note, also, that you won't be able to use as?
any more, you'll have to cast it using as?
and handle the nil possibility, here's one approach:
func doSomethingOnDrag(sender: UIButton, event: UIEvent) {
if let touch = event.touchesForView(sender)?.first as? UITouch,
location = touch.locationInView(sender) {
// use location
}
}
这篇关于event.touchesForView().AnyObject() 在 Xcode 6.3 中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!