1

This is by far the strangest thing thats happened to me. So I have the code below in a unit test...

let aStoryboard = UIStoryboard(name: "myStoryboard", bundle: nil)
let viewController = aStoryboard.instantiateViewControllerWithIdentifier("myViewController") as? CustomViewController

The code above is executed and viewController is nil, okay that can happen (it shouldn't be because both identifiers exist and files are linked to test targets) but yeah okay.

Here's the crazy part, when I add a breakpoint, step over the lines above and then type the following into lldb

viewController = aStoryboard.instantiateViewControllerWithIdentifier("myViewController") as? CustomViewController

viewController is allocated with memory. What how? If I manually add this in lldb then it has memory if I don't then it doesn't have memory and it's nil for the unit test.

Someone help me!

harmeet07
  • 501
  • 1
  • 5
  • 11

1 Answers1

0

"viewController is allocated with memory. What how? If I manually add this in lldb then it has memory if I don't then it doesn't have memory and it's nil for the unit test."

In Swift nil doesn't mean the same as Objective-C. It's not a pointer to NULL. It just mean absence of proper value. More information here: null / nil in swift language

So answering your question: it's fine to have memory for nil variable.

Community
  • 1
  • 1
Jurasic
  • 1,845
  • 1
  • 23
  • 29
  • The problem isn't it is nil, objects can be nil thats fine. If I ran the code above without a breakpoint viewController is nil, that's fine. But if i didn't make any code changes, added a breakpoint. When the program executes the code above, this time around viewController has now got memory and a value, that's the strange behaviour. I didn't make a code change I simply added a breakpoint and attached the debugger. Why does viewController have a value when the debugger is attached? – harmeet07 Mar 03 '17 at 12:37
  • Could you please post a screenshot of this case? It may help us to clarify what is the issue. – Jurasic Mar 20 '17 at 14:16