iosxcodeswiftuitouchuievent

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


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?


Solution

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