aStoryboard.instantiateViewControllerWithIdentifier(“myid”)在lldb中返回nil但不是nil

到目前为止,这是我遇到的最奇怪的事情。 所以我在unit testing中有以下代码……

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

执行上面的代码并且viewController是nil,好吧可能发生(它不应该是因为两个标识符都存在而且文件链接到测试目标)但是没关系。

这是一个疯狂的部分,当我添加一个断点,跨过上面的行,然后在lldb中键入以下内容

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

viewController分配了内存。 怎么样? 如果我在lldb中手动添加它,那么它有内存,如果我没有那么它没有内存,它是unit testing的零。

谁来帮帮我!

“viewController分配了内存。怎么怎么办?如果我在lldb中手动添加它,那么它有内存,如果我没有那么它没有内存,它就是unit testing的nil。”

在Swift中,nil与Objective-C并不相同。 它不是指向NULL的指针。 这只是意味着没有适当的价值。 更多信息: 快速语言中的null / nil

所以回答你的问题:为nil变量保留内存很好。