`

Objective-c 中 nil, Nil, NULL和NSNull的区别

 
阅读更多

nil: A null pointer to an Objective-C object.
( #define nil ((id)0) )

Nil: A null pointer to an Objective-C class.

NULL: A null pointer to anything else, is for C-style memory pointers.
( #define NULL ((void *)0) )


NSNull: A class defines a singleton object used to represent null values in collection objects (which don't allow nil values).
[NSNull null]: The singleton instance of NSNull.

Technically they're all the same,,, but in practice they give someone reading your code some hints about what's going on; just like naming classes with a capital letter and instances with lowercase is recommended, but not required.

If someone sees you passing NULL, they know the receiver expects a C pointer. If they see nil, they know the receiver is expecting an object. If they see Nil, they know the receiver is expecting a class. Readability.

if obj is nil , [obj message] will return NO, without NSException
if obj is NSNull , [obj message will throw a NSException


Demo1:

       [NSApp beginSheet:sheet
              modalForWindow:mainWindow

              modalDelegate:nil       //pointing to an object 
              didEndSelector:NULL     //pointing to a non object/class 
              contextInfo:NULL];      //pointing to a non object/class


Demo2:

        NSObject *obj1;
        if (obj1 != nil) {
            NSLog(@"object is not nil");
        }else
        {
            NSLog(@"object is nil");
        }
        
        testClass *c1;
        if (c1 != Nil) {
            NSLog(@"class is not Nil");
        }else
        {
            NSLog(@"class is Nil");
        }
        
        int *money;
        if (money != NULL) {
            NSLog(@"money is not NULL");
        }else
        {
            NSLog(@"money is NULL");
        }


Demo3:

        NSObject *obj1 = [[NSObject alloc] init];
        NSObject *obj2 = [NSNull null];
        NSObject *obj3 = [NSObject new];
        NSObject *obj4;
        NSArray *arr1 = [NSArray arrayWithObjects:obj1, obj2, obj3, obj4, nil];
        NSLog(@"arr1 count: %ld", [arr1 count]);    //arr1 count: 3


        NSObject *obj1;
        NSObject *obj2 = [[NSObject alloc] init];
        NSObject *obj3 = [NSNull null];
        NSObject *obj4 = [NSObject new];
        NSArray *arr2 = [NSArray arrayWithObjects:obj1, obj2, obj3, obj4, nil];
        NSLog(@"arr2 count: %ld", [arr2 count]);   //arr2 count: 0


Demo4:

        //有异常!
        NSObject *obj1 = [NSNull null];
        NSArray *arr1 = [NSArray arrayWithObjects:@"One", @"TWO", obj1, @"three" ,nil];
        for (NSString *str in arr1) {
            NSLog(@"array object: %@", [str lowercaseString]);
        }

        //修改
        NSObject *obj1 = [NSNull null];
        NSArray *arr1 = [NSArray arrayWithObjects:@"One", @"TWO", obj1, @"three" ,nil];
        for (NSString *str in arr1) {
            if (str != [NSNull null]){
                NSLog(@"array object: %@", [str lowercaseString]);
            }
        } 

分享到:
评论

相关推荐

    ob<x>jective-C学习资料汇总专题

    资源名称:Objective-C学习资料汇总专题资源目录:【】Objective-C NSAutoreleasePool【】Objective-c 中 nil, Nil, NULL和NSNull的区别【】Objective-C 中self 和 super【】Objective-C 的编程之道【】Objective-C_...

    总结IOS中nil、Nil、NULL和NSNull区别

    相信有不少朋友想知道,在 Objective-C 中 nil 和 Nil 以及 NULL 的区别。最重要的是,在面试中还有不少朋友常会被问到。现在小编在这里统一详细说明。

    [Objective-c程序设计].杨正洪等.扫描版

    《Objective-C程序设计》(作者杨正洪、郑齐心、李建国)通过大量的实例系统地介绍了Objective-C语言的基本概念、语法规则、框架、类库及开发环境。读者在阅读本书后,可以掌握Objective-C语言的基本内容,并进行...

    Objective-C培训资料

    Object_C 中的 nil 相当于 NULL 。 Object_C 中的 YES 和 NO 相当于 true 和 false 。 这里再讲解一下 YES 和 NO : Object-c 提供了 BOOL 类型,但这个 BOOL 类型和 C++ 里的并不一样:在 C++ 里一切非 0 值的东西...

    Google Objective-C Style Guide 中文版

    Google Objective-C Style Guide 中文版 目录 例子 空格与格式 空格与制表符 行宽 方法声明与定义 方法调用 @public与@private 异常 协议 命名 文件名 Objective-C++ 类名 分类名 Objective-C方法名 变量名 注释 ...

    Using Swift with Cocoa and Objective-C完整中文CocoaChina精校版

    在同一工程中使用 在同一工程中使用 在同一工程中使用 在同一工程中使用 SwiftSwift Swift 和 Objective Objective Objective Objective-C . 39 Mix and Match Mix and Match Mix and Match Mix and Match Mix and ...

    iOS中nil、Nil、NULL、NSNull详解

    主要介绍了iOS中nil、Nil、NULL、NSNull详解的相关资料,需要的朋友可以参考下

    IOS 基础之nil,NULL,NSNULL区别详解

    IOS 基础之nil,NULL,NSNULL区别详解 ① nil:一般赋值给空对象。 ② NULL:NULL 是一个通用指针(泛型指针)。 一般赋值给 nil 之外的其他空值。如SEL等。 ③ NSNULL:[NSNull null] 是一个对象,他用在不能使用 ...

    Objective-c对象组装XML

    [map setObject:@"c" forKey:@"content"]; 或者 NSMutableArray *list = [[NSMutableArray alloc]init]; NSMutableDictionary *map1 = [[NSMutableDictionary alloc]init]; [map1 setObject:@"a1" forKey:@...

    objective-c小技巧

    objective-c小技巧 1. 使用@property和@synthesize声明一个成员变量,给其赋值是时要在前面加上"self.",以便调用成员变量的setmember方法。 直接调用成员变量并且给其赋值:member=[NSString stringWithFormat...

    overnote#over-golang#04-避坑-3-nil判断1

    接口在底层的实现有两个部分,type 和 data:- 显式地将nil赋值给接口时:接口 == nil,此时接口的type和data都为nil。type Stu

    node-no-nil:从对象和数组中删除nil值。 将所有其他nil值转换为null

    var noNil = require ( 'no-nil' ) ; var input = [ 1 , undefined ] ; console . log ( noNil ( input ) ) ; // [1]; ##执照 The MIT License (MIT) Copyright (c) 2015 Joseph Spencer Permission is hereby ...

    IGScraperKit:在 Objective-C 或 Ruby 中创建动态网页抓取工具!

    在 Objective-C 或 Ruby 中创建动态网络爬虫。 用法 创建一个刮板: # import " IGScraperKit.h " IGScraper* scraper = [IGScraper scraperWithBlock: ^ id (IGXMLNode* node, NSString * url) { return [[[node...

    lua-cjson decode中null改成nil

    lua-cjson lua的cjson库 decode略做修改 结果中null还原成nil

    object c 编程

    n nil:在 C/C++ 你或许曾使用过 NULL,而在 Objective-C 中则是 nil。不同之 处是你可以传递讯息给 nil(例如 [nil message];),这是完全合法的,然而你 却不能对 NULL 如法炮制。 n BOOL:C 没有正式的布尔型别,...

Global site tag (gtag.js) - Google Analytics