Hartix
Hartix

Reputation: 330

Error with SKAction.runBlock

Here is some simple code from a SpriteKit test app I'm playing with:

let wait = SKAction.waitForDuration(1)
let perform = SKAction.runBlock({self.checkAnswer()})
let checkAnswerSeq = SKAction.sequence([wait, perform])

I'm getting a "'SKAction' is not convertible to '(Selector, onTarget: AnyObject!) -> SKAction!'" error on the second line. Any ideas?

Upvotes: 2

Views: 1207

Answers (1)

Milos
Milos

Reputation: 2758

The problem arises from the fact that your runBlock is a single expression closure, where compiler implies a return statement. So, the required type of the runBlock is () -> (), whereas I suspect that your self.checkAnswer() is returning a value (judging by the method name, perhaps a Bool). In other words, you are returning a Bool where a Void is expected. If instead you write an additional expression, the error should go away:

let perform = SKAction.runBlock { self.checkAnswer(); return () }

or, really, any other expression:

let perform = SKAction.runBlock { self.checkAnswer(); 42 }

or, if you consume the returned value:

let perform = SKAction.runBlock { let x = self.checkAnswer() }

or simply:

let perform = SKAction.runBlock { _ = self.checkAnswer() }

Some people consider this to be a bug. However, the error is exactly consistent with the rest of the language. In other words, the return type conflict should not go without an error, just as it shouldn't in any other situation.

Upvotes: 3

Related Questions