Objective-C:为什么要检查!= nil [英] Objective-C: Why should one check for != nil

查看:97
本文介绍了Objective-C:为什么要检查!= nil的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我经常看到这样的代码:

I often see code like this:

// Approach 1
if(data != nil){
    // Do this and that
}

当人们可以像这样简单地进行检查时:

When one could simply do the check like this:

// Approach 2
if(data){
    // Do this and that
}

在我看来,第二种方法似乎更好,因为它更短且更易于阅读.

To me the second approach seems to be much better since it is shorter and easier to read.

那为什么要完全使用第一种方法呢?

So why should one use the first approach at all?

推荐答案

这主要是样式首选项,或者某些人不知道可以使用第一个解决方案.

This is mostly a style preference, or some people does not know that it is possible to use first solution.

此外,它们是Java之类的语言,您无法编写第二种解决方案,必须编写yourVar!= null

Moreover, they are languages like java where you can't write the second solution, You must write yourVar != null

这篇关于Objective-C:为什么要检查!= nil的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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