override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
let touch = touches.anyObject() as UITouch
let touchLocation = touch.locationInNode(self)
timer = NSTimer.scheduledTimerWithTimeInterval(0.5, target: self, selector: "shoot", userInfo: touchLocation, repeats: true) // error 1
}
func shoot() {
var touchLocation: CGPoint = timer.userInfo // error 2
println("running")
}
我正在尝试创建一个定期运行的计时器,它将触摸点(CGPoint)作为userInfo传递给NSTimer,然后通过shoot()函数访问它。但是,现在我收到的错误是
1)调用中的额外参数选择器
2)无法转换表达式AnyObject?致CGPoint
现在我似乎无法将userInfo传递给另一个函数然后检索它。
答案 0 :(得分:2)
不幸的是CGPoint
不是一个对象(至少在Objective-C世界中,Cocoa API起源于此)。它必须包装在NSValue
对象中以放入集合中。
override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
let touch = touches.anyObject() as UITouch
let touchLocation = touch.locationInNode(self)
let wrappedLocation = NSValue(CGPoint: touchLocation)
timer = NSTimer.scheduledTimerWithTimeInterval(0.5, target: self, selector: "shoot:", userInfo: ["touchLocation" : wrappedLocation], repeats: true)
}
func shoot(timer: NSTimer) {
let userInfo = timer.userInfo as Dictionary<String, AnyObject>
var touchLocation: CGPoint = (userInfo["touchLocation"] as NSValue).CGPointValue()
println("running")
}