Search code examples
xcodebuild-errorswift2

Swift 2.0 Errors


I am trying to use the following code:

func redrawShape(shape: Shape, completion:() -> ()) {
    for (idx, block) in shape.blocks.enumerate() {
        let sprite = block.sprite!
        let moveTo = pointForColumn(block.column, row: block.row)
        let moveToAction: SKAction = SKAction.moveTo(moveTo, duration: 0.05)
        moveToAction.timingMode = .EaseOut
        sprite.runAction(moveToAction, completion: nil)
}

I get an error at this line:

sprite.runAction(moveToAction, completion: nil)

The error says:

Cannot invoke 'runAction' with an argument list of type '(SKAction, completion: nil)'

I do not understand how to fix this.


Solution

  • The completion handler is not an optional. You need to pass something. You can pass an empty closure:

    sprite.runAction(moveToAction, completion: {})
    

    Or, as matt points out, the better approach is to use the other form:

    sprite.runAction(moveToAction)
    

    Matt's answer is really the better one.