Swift 2.0 Errors

229 views Asked by At

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.

2

There are 2 answers

2
Rob Napier On BEST ANSWER

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.

0
matt On

You are calling the wrong method. If you have no completion handler, then do not call runAction(_:completion:). Call simple runAction(_:), like this:

 sprite.runAction(moveToAction)

In other words, you have two options:

  • You can call runAction(_:) if you have no completion handler, or

  • you can call runAction(_:completion:) if you have a completion handler.

But what you cannot do is call runAction(_:completion:) if you have no completion handler - as you are trying to do.