14

I'm diving into Swift lang by Apple and have some problems using the trailing closure syntax, example:

func test(txt: String, resolve: (name: String) -> Void) { resolve(name: "Dodo") } // Errors here complaining on resolve param test("hello", (name: String) { println("callback") }) 

How to fix it?

1 Answer 1

27

you have the wrong closure syntax

test("hello", {(name: String) in println("callback") }) 

or

test("hello", { println("callback: \($0)") }) 

or

test("hello") {(name: String) in println("callback") } 

or

test("hello") { println("callback: \($0)") } 
Sign up to request clarification or add additional context in comments.

4 Comments

I have one more question, this resolve(name: name) throws an error use of unresolved identifier 'name'.. how to pass arguments in callback call?
hi i dont get that. can you share the code that fails?
I have a question... In the first code example that you provided above, you have "{(name: String) in". What's the semantic meaning of "in" when used in this context?
In is the separator between parameters and statements

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.