Tsallaka zuwa abun ciki
Justice Tech School

defer

defer runs code when the scope ends, great for cleanup.

Example
func work() {
    print("start")
    defer { print("cleanup") }
    print("end")
}
work()

Exercise

Run at scope end.

 { cleanup() }

Quiz Test

When does defer run?

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.