Skip to content

Instantly share code, notes, and snippets.

@yuu
Last active November 8, 2019 04:31
Show Gist options
  • Save yuu/013aab57a3c3179215f4cae17adacbac to your computer and use it in GitHub Desktop.
Save yuu/013aab57a3c3179215f4cae17adacbac to your computer and use it in GitHub Desktop.
[swift] why? compiler warnings
xcode:11
target/platform:iOS 13.1
swift:5
warning:'defer' statement before end of scope always executes immediately; replace with 'do' statement to silence this warning
fix-suggest:Replace 'defer' with 'do'
func doSomething(value: Int?, value2: Int?) {
    guard let value = value else { return }
    defer {
        print(value2 ?? "nil")
    }
}

doSomething(value: nil, value2: 1) // result:
doSomething(value: 1, value2: nil) // result: nil
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment