UIStoryboard() 中的 nil 和 NSBundle 有什么区别?

What's the difference between nil and NSBundle in UIStoryboard()?

我经常看到这两种用法,但我找不到它们的区别:

let storyboard: UIStoryboard = UIStoryboard(name: "Main", bundle: NSBundle.mainBundle())

let storyboard: UIStoryboard = UIStoryboard(name: "Main", bundle:nil)

那么将 bundle 设置为 nilNSBundle.mainBundle() 有什么区别?

其实这两者没有区别。无论您指定 nil 还是 NSBundle.mainBundle() 都将引用主包。

The bundle containing the storyboard file and its related resources. If you specify nil, this method looks in the main bundle of the current application.

参考:UIStoryboard Class Reference

当您需要从 mainBundle() 之外的另一个包访问信息时,bundle: 参数将变得有用。例如,许多第三方库都附带了自己的包,用于打包该特定库使用的资源。因此,如果您想访问这些资源,您需要引用该资源包。