将两个nil参数传递给initWithNibName:bundle:method bad practice(即不安全或较慢)? [英] Is passing two nil paramters to initWithNibName:bundle: method bad practice (i.e. unsafe or slower)?

查看:155
本文介绍了将两个nil参数传递给initWithNibName:bundle:method bad practice(即不安全或较慢)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据 UIViewController 中关于 initWithNibName:bundle:

的评论

如果使用nil nib名称调用此方法,则此类'-loadView方法将尝试加载一个名称与您的视图控制器类相同的NIB。



我总是把我的nibs命名为视图控制器。



这是一个坏习惯(即不安全,更慢,或可能导致问题的路上)只是传递nil两个参数,而不是一个nib名称字符串? / p>

想要这样做的主要驱动力是我发现在Xcode中使用refactor选项不会重命名nib名称字符串(只有类名称, 're used)。

解决方案

这不是不安全或缓慢的。它只是意味着将有一个额外的检查,看到与您的类具有相同的名称的NIB确实存在于捆绑,那么它将被加载,就像你已经通过NIB名称。它不会导致问题,除非你决定更改如何命名您的NIB。表面上,有一个整个讨论 a>关于如何 -initWithNibName:bundle:是一个坏的初始化程序,只是使用 -init p>

According to a comment in UIViewController regarding initWithNibName:bundle:

If you invoke this method with a nil nib name, then this class' -loadView method will attempt to load a NIB whose name is the same as your view controller's class.

I always name my nibs the same as the view controller.

Is it a bad practice (i.e. unsafe, slower, or likely to cause problems down the road) to just pass nil to both parameters instead of a nib name string?

The main driving desire behind wanting to do this is I've found that using the refactor option in Xcode doesn't rename the nib name strings (only the class names wherever they're used). Thereby, this causes crashes if one isn't careful to go back and rename these everywhere.

解决方案

It's not unsafe or slow. It just means that there will be an extra check to see that the NIB with the same name as your class really exists in the bundle, then it will be loaded exactly as if you had passed the NIB name. It won't cause problems unless you decide to change how you name your NIBs. In face, there's a whole discussion about how -initWithNibName:bundle: is a bad initializer, and just using -init is better.

这篇关于将两个nil参数传递给initWithNibName:bundle:method bad practice(即不安全或较慢)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆