什么时候在Objective-C中使用nil和NULL? [英] When should I use nil and NULL in Objective-C?

查看:82
本文介绍了什么时候在Objective-C中使用nil和NULL?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是示例代码:

NSDictionary *myDictionary = [NSDictionary dictionary];
NSNumber *myNumber = [myDictionary valueForKey: @"MyNumber"];
NSLog(@"myNumber = %@", myNumber); // output myNumber = (null)

if (myNumber == nil)
    NSLog(@"test 1 myNumber == nil");

if (myNumber == NULL)
    NSLog(@"test 2 myNumber == NULL");

if ([myNumber isEqual:[NSNull null]])
    NSLog(@"test 3 myNumber == [NSNull null]");

何时应使用nil,NULL和[NSNull null]?

When should I use nil, NULL and [NSNull null]?

推荐答案

您可以在任何可以使用null的地方使用nil.主要区别在于您可以将邮件发送到nil,因此可以在null无法使用的某些地方使用它.

You can use nil about anywhere you can use null. The main difference is that you can send messages to nil, so you can use it in some places where null cant work.

通常,只需使用nil.

这篇关于什么时候在Objective-C中使用nil和NULL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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