1
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 を変換できませんか? CGポイントへ

現在、userInfo を他の関数に渡して取得することはできないようです。

4

1 に答える 1

2

残念ながらCGPoint、オブジェクトではありません (少なくとも、Cocoa API の元となった Objective-C の世界では)。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")
}
于 2014-11-25T17:58:55.557 に答える