02. Lazy loading and Anonymous function

It will take about 1 minutes to finish reading this article.

In Swift, we use the lazy keyword before the property to simply implement delayed loading, for example:

1
2
3
4
5
lazy var str: String = {
let str = "Hello"
print("Only access the output for the first time")
return str
}()

This style of writing is similar to closure, but it is actually an kind of anonymous function. We can use anonymous functions to initilize data. As shown below, it is an anoymous function creation call operation:

1
2
3
{
//anonymous function code
}()

We can try to print the type of this type.

1
2
3
4
5
6
7
8
9
func test() {

}
func TestCase() {
let a: () = {}()
let b = test
print(type(of: a))
print(type(of: b))
}

The result is:

1
2
()
() -> ()