CFRJ
CFRJ

Reputation: 157

What does let someThing = {} mean?

I've seen this in the book I'm reading but I don't know what it means and what it does. Is it something like a function? I've tried looking at the Swift language book by Apple but could not find the answer.

Thanks

Upvotes: 0

Views: 128

Answers (2)

D4ttatraya
D4ttatraya

Reputation: 3412

This syntax is mostly used for declaring closure in Swift.

e.g.

let something = { print("something") }
something()//prints 'something"

Here we are declaring closure named something and then calling it later.
We can declare closures with parameters also as:

let something = { (str: String) in
    print("something param: \(str)")
}

something("ok")//prints "something param: ok"

We can even declare closures with return value as:

let something = { (str: String) -> Bool in
    print("something param: \(str)")
    return true
}

let success = something("ok")//prints "something param: ok" and return true

Upvotes: 3

AlexWoe
AlexWoe

Reputation: 854

Maybe you should do the swift basics first. If you want to learn more about closures you can have a look at the following side:

https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/Closures.html

Upvotes: 2

Related Questions