Why lldb change Swift's temp var does not work?

Viewed 70

There is a temp var tmp as a NSObject instance, I added a breakpoint, which does express tmp = nil before print action, and I can see this tmp really became nil in the vars list.

However, it still printed the old NSObject instance, why?

Xcode Screenshot

1 Answers

For technical reasons having to do with how swift resolves the conflict between being a "safe" language, which requires a lot of bookkeeping, and being "swift" which requires optimization of the bookkeeping work even at -Onone, the compiler can only present an "always available" local variable to lldb by making a (from its perspective) read-only copy of the variable, and pointing the debugger at that. So in many cases, the debugger doesn't know where the in-use version of the variable lives, and changes to the debugger copy don't affect the program execution.

That is what you are seeing here.

Related