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. }  
  • 相关阅读:
    OpenMP并行程序设计
    Snmp在Windows下的实现WinSNMP编程原理
    利用C#开发基于snmpsharpnet基础的SNMP开发应用
    SNMP用VC实现的方法
    题目
    C# combox问题
    网络管理Snmp
    error BK1506
    响应activex事件
    使用C# 连接不同版本的Oracle.DataAccess
  • 原文地址:https://www.cnblogs.com/hanjun/p/3029790.html
Copyright © 2011-2022 走看看