问题
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