zoukankan      html  css  js  c++  java
  • ios nil、NULL和NSNull 的使用

    nil用来给对象赋值(Objective-C中的任何对象都属于id类型),NULL则给任何指针赋值,NULL和nil不能互换,nil用于类指针赋值(在Objective-C中类是一个对象,是类的meta-class的实例), 而NSNull则用于集合操作,虽然它们表示的都是空值,但使用的场合完全不同。

    示例如下:

    1. id object = nil;  
    2. // 判断对象不为空  
    3. if (object) {  
    4. }  
    5.       
    6. // 判断对象为空  
    7. if (object == nil) {  
    8. }  
    9.           
    10. // 数组初始化,空值结束  
    11. NSArray *array = [[NSArray alloc] initWithObjects:@"First", @"Second", nil];  
    12.   
    13. // 判断数组元素是否为空  
    14. NSString *element = [array objectAtIndex:2];  
    15. if ((NSNull *)element == [NSNull null]) {  
    16. }  
    17.   
    18. // 判断字典对象的元素是否为空  
    19. NSDictionary *dictionary = [NSDictionary dictionaryWithObjectsAndKeys:  
    20.     @"iPhone", @"First", @"iPad", @"Second", nil];  
    21. NSString *value = [dictionary objectForKey:@"First"];  
    22. if ((NSNull *)value == [NSNull null]) {  
    23. }  
  • 相关阅读:
    ACdream 1114(莫比乌斯反演)
    ACdream 1148(莫比乌斯反演+分块)
    bzoj2301(莫比乌斯反演+分块)
    hdu1695(莫比乌斯反演)
    hdu4908(中位数)
    bzoj1497(最小割)
    hdu3605(最大流+状态压缩)
    【Leetcode】Add Two Numbers
    【Leetcode】Add Binary
    【Leetcode】Single Number II
  • 原文地址:https://www.cnblogs.com/hanjun/p/3029790.html
Copyright © 2011-2022 走看看