sprite kit - Running methods inside a block in Swift -


this dumb question, still... i'm calling function inside block this:

let makerocks = skaction.sequence([skaction.runblock(self.createmynode),<---- here should ()                                     skaction.waitforduration(0.1, withrange: 0.15)])  func createmynode() {     // blabla } 

i don't it. there should parentheses after self.createmynode still compiles. how so?

you're not in fact calling function, createmynode called inside skaction.runblock, you're passing argument.

take @ type skaction.runblock accepts, skaction documentation:

class func runblock(_ block: dispatch_block_t) -> skaction 

and gcd documentation:

typealias dispatch_block_t = () -> void 

therefore, skaction.runblock accepts function (or closure, they're same thing), takes no arguments , returns void; you're suppling createmynode.

with information it's clear see why don't add parentheses after createmynode - because call function, passing void (the return type of createmynode) skaction.runblock, skaction.runblock won't accept.


to clarify, here's example. had function:

func takesafunc(c: (int, int) -> int) -> void {     print(c(1, 2)) } 

and wanted pass in function added 2 numbers. could write:

takesafunc { num1, num2 in     num1 + num2 }  // prints: 3 

but alternatively, pass in + operator accepts ints - that's function too. take @ definition:

func +(lhs: int, rhs: int) -> int 

that matches type required takesafunc, therefore can write:

takesafunc(+) // prints: 3 

Comments

Popular posts from this blog

java - Andrioid studio start fail: Fatal error initializing 'null' -

android - Gradle sync Error:Configuration with name 'default' not found -

StringGrid issue in Delphi XE8 firemonkey mobile app -