event.touchesForView().AnyObject() not working in Xcode 6.3

ぐ巨炮叔叔 提交于 2019-12-23 21:12:42

问题


This worked perfectly before:

func doSomethingOnDrag(sender: UIButton, event: UIEvent) {
    let touch = event.touchesForView(sender).AnyObject() as UITouch
    let location = touch.locationInView(sender)
}

But in Xcode 6.3, I now get the error:

Cannot invoke 'AnyObject' with no arguments

How do I fix this?


回答1:


In 1.2, touchesForView now returns a native Swift Set rather than an NSSet, and Set doesn't have an anyObject() method.

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



回答2:


func doSomethingOnDrag(sender: UIButton, event: UIEvent) {
    let buttonView = sender as! UIView;
    let touches : Set<UITouch> = event.touchesForView(buttonView)!
    let touch = touches.first!
    let location = touch.locationInView(buttonView)
}


来源:https://stackoverflow.com/questions/29566861/event-touchesforview-anyobject-not-working-in-xcode-6-3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!