aStoryboard.instantiateViewControllerWithIdentifier(“myid”) returns nil but not nil in lldb

不羁岁月 提交于 2020-01-10 06:07:29

问题


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!


回答1:


"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.



来源:https://stackoverflow.com/questions/38959445/astoryboard-instantiateviewcontrollerwithidentifiermyid-returns-nil-but-not

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!