Home > AI > Language > Swift >

defer

Example 1:

print("Step 1")

do {
    defer { print("Step 2") }
    print("Step 3")
    print("Step 4")
}

print("Step 5")

// 1, 3, 4, 2, 5

Example 2:

func printStringNumbers() {
    defer { print("1") }
    defer { print("2") }
    defer { print("3") }

    print("4")
}

// 4, 3, 2, 1

Leave a Reply