event.touchesForView().AnyObject() not working in Xcode 6.3
Asked Answered
H

2

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?

Heavyweight answered 10/4, 2015 at 16:57 Comment(0)
I
6

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
    }
}
Icterus answered 10/4, 2015 at 18:0 Comment(0)
B
0
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)
}
Bandylegged answered 22/6, 2015 at 12:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.