NSNull作用

来源:互联网 发布:隐形眼镜 品牌 知乎 编辑:程序博客网 时间:2024/05/02 04:24
Represent nil with NSNull  (摘自官方文档)
It’s not possible to add nil to the collection classes described in this section because nil in Objective-C means “no object.” If you need to represent “no object” in a collection, you can use the NSNull class:


    NSArray *array = @[ @"string", @42, [NSNull null] ];
NSNull is a singleton class, which means that the null method will always return the same instance. This means that you can check whether an object in an array is equal to the shared NSNull instance:


    for (id object in array) {
        if (object == [NSNull null]) {
            NSLog(@"Found a null object");
        }
    }


在NSArray   NSDictionary中 nil代表集合类的结束,但是我们又想在集合中添加空位,以备将来用,我们就使用


NSNull 类  它是一个单例类,就是代表一个空对象。
0 0