i have the following code:
import SpriteKit
import Foundation
class GameScene: SKScene {
var occupiedCoordinates: NSMutableArray = NSMutableArray()
func addShape () {
//...
shape.position = CGPoint(x:actualX, y:actualY)
self.occupiedCoordinates.addObject(NSValue(CGPoint:shape.position))
let halfDuration = random(min: CGFloat(0.5), max: CGFloat(5))
//...
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 2*halfDuration), dispatch_get_main_queue(), ^{
self.occupiedCoordinates.removeObjectAtIndex(0)
});
}
}
i used the original snipped GCD: Dispatch After
and i get the following message in the line of dispatch_after()
'^' is not a prefix unary operator
any ideas what the problem is?
This is the correct way to pass a closure (i.e. an objective-c block) to a function in swift:
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 2 * halfDuration), dispatch_get_main_queue(), { () -> () in
self.occupiedCoordinates.removeObjectAtIndex(0)
})
or you can also use this compact form:
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 2 * halfDuration), dispatch_get_main_queue()) {
self.occupiedCoordinates.removeObjectAtIndex(0)
}
Suggested reading: Closures