The Swift “defer” operator my not work exactly like you expect. It’s not exactly the last code executed before the function returns. Well, it kind of is. But also not so much. 🤦🏻♂️ As this article says, “defer” basically happens at the “ret” in “return”. 🤯
It takes a while to process what’s going on here, so take your time…
https://medium.com/@sergeysmagleev/how-defer-operator-in-swift-actually-works-30dbacb3477b
It’s also fascinating to see how what some very simple Swift code looks like when decompiled. Temporary registers. Global var access. Code injection in all the wrong places. 🤯 I have newfound respect for compiler writers.
And thanks to Sergey Smagleev on Medium for a great deep dive into “defer”.
Whoa, a simple three-liner has turned into this abomination that spans 34 lines of code.

