Romulan233
Romulan233

Reputation: 13

How do I pass a closure as a parameter in swift (just the basics)

So, I just started learning swift yesterday, so bear with me. I'm working on closures. I have a very simple set of statements.


    let myClosure = {
        println("this is a simple test")
    }

    func showWork( closure : ()->() ) {
      closure()
    }

showWork(myClosure)

I know that I am doing something wrong because println isn't working in the xCode playground. Basically, I've created a simple closure and passed it to my function. But, println isn't printing. What am I doing wrong?

Upvotes: 0

Views: 108

Answers (1)

Brendan Whiting
Brendan Whiting

Reputation: 504

Instead of () -> () write Void -> Void. So the whole thing would look like this:

let myClosure = {
    print("this is a simple test")
}

func showWork( closure : Void->Void ) {
    closure()
}

showWork(myClosure)

Upvotes: 1

Related Questions