Swift Language The Defer Statement When NOT to use a defer statement

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Insert
> Step 2: And Like the video. BONUS: You can also share it!


When using a defer-statement, make sure the code remains readable and the execution order remains clear. For example, the following use of the defer-statement makes the execution order and the function of the code hard to comprehend.

postfix func ++ (inout value: Int) -> Int {
    defer { value += 1 } // do NOT do this!
    return value

Got any Swift Language Question?