iOS:初始值、零值、空值(一)--变量及判断

来源:互联网 发布:中国古典风格网站源码 编辑:程序博客网 时间:2024/06/05 16:20

测试环境:xcode 6.1 + ios sdk 8.0;

         初始值:变量声明后的值,可能零、空、其他等。

         零值:基本类型指0/0.0;对象指创建/初始化后,值是“零”,但对象存在;

         空值:对象不存在,没有创建(只对对象而言);它的值有可能是nil(可用作判断等使用),有可能是非法地址(不能使用);

         总的来说

      变量初始值

        (1)在方法内声明(局部变量):

  • 对于基本类型,在给它赋值前,它没有值的概念,不要以为是零值,或者用它做if条件里的判断,
  • 对于对象,现在测试发现,它的初值为nil,可以用它用if条件里判断;但以前老版本测试发现,声明后,它没有赋值/初始化前,为“空”值的概念,不能用它做if的条件判断,或者NSLog输出用,否则程序crash。

       (2)在类中声明(类成员):

  • 对于基本类型,值为0/0.0;可用if条件判断等;
  • 对于对象,值为nil;可用if条件判断等;
        总的来说:不管什么类型,哪里声明的,要想用,或者if条件里用,用之前先创建并初始化,养成良好习惯;

@interface ViewController (){        int memberInt;    UIView *memberObj;     }@end@implementation ViewController- (void)viewDidLoad {    [super viewDidLoad];    // Do any additional setup after loading the view, typically from a nib.         int localInt;    UIView *localObj;    // 基本类型  局部    if (0 == localInt) {        NSLog(@"localInt value 0");    }    if (localInt) {        NSLog(@"localInt value not 0");    }    NSLog(@"localInt:%d",localInt);        // 基本类型 类成员    if (0 == memberInt) {        NSLog(@"memberInt value 0");    }    if (memberInt) {        NSLog(@"memberInt value not 0");    }    NSLog(@"memberInt:%d",memberInt);         // 对象  局部    if (0 == localObj) {        NSLog(@"localObj value 0");    }    if (nil == localObj) {        NSLog(@"localObj value nil");    }    if (localObj) {        NSLog(@"localObj value not nil");    }    NSLog(@"localObj:%@",localObj);            // 对象  类成员    if (0 == memberObj) {        NSLog(@"memberObj value 0");    }    if (nil == memberObj) {        NSLog(@"memberObj value nil");    }    if (memberObj) {        NSLog(@"memberObj value not nil");    }    NSLog(@"memberObj:%@",memberObj);
2014-11-01 12:57:21.260 AlwaysTest[4837:60b] localInt value not 02014-11-01 12:57:21.264 AlwaysTest[4837:60b] localInt:9949055292014-11-01 12:57:21.266 AlwaysTest[4837:60b] memberInt value 02014-11-01 12:57:21.267 AlwaysTest[4837:60b] memberInt:02014-11-01 12:57:21.269 AlwaysTest[4837:60b] localObj value 02014-11-01 12:57:21.271 AlwaysTest[4837:60b] localObj value nil2014-11-01 12:57:21.272 AlwaysTest[4837:60b] localObj:(null)2014-11-01 12:57:21.274 AlwaysTest[4837:60b] memberObj value 02014-11-01 12:57:21.275 AlwaysTest[4837:60b] memberObj value nil2014-11-01 12:57:21.277 AlwaysTest[4837:60b] memberObj:(null)

      零值

         空值nil,NULL,Nil,NSNull

--nil是Oc中对象指针的空值,常用在给对象赋初值。

--NULL是基本数据类型指针的空值,例如:int *k = NULL;

--Nil是类指针为空;

--NSNull is a class for objects that represent null. In fact, there’s only one object, namely the one returned by +[NSNull null]. It is different from nil because nil is a literal null value, i.e., it isn’t an object. The single instance of NSNull, on the other hand, is a proper object.

--NSNull is often used in Foundation collections since they cannot store nil values. In the case of dictionaries, -objectForKey: returns nil to indicate that a given key has no corresponding object in the dictionary, i.e., the key hasn’t been added to the dictionary. If you want to make it explicit that you have a certain key but it doesn’t have a value yet, you can use [NSNull null].For instance, the following throws an exception because dictionaries cannot store nil values:

    NSMutableDictionary *dict = [NSMutableDictionary dictionary];     [dict setObject:nil forKey:@"someKey"];     On the other hand, the following code is valid since [NSNull null] is a non-nil object:     NSMutableDictionary *dict = [NSMutableDictionary dictionary];     [dict setObject:[NSNull null] forKey:@"someKey"]; // It’s worth mentioning that Foundation collections have initialisers that use nil as a marker for the end of a list of objects without having to specify the number of elements in the list. This can only happen because nil cannot be stored in a Foundation collection. For instance,     NSArray *array = [NSArray arrayWithObjects:@"one", @"two", nil];

         例子测试:
    UIView *view;    view = nil;        if (0 == view) {        NSLog(@"view can equal 0 ");    }        if (NULL == 0) {        NSLog(@"NULL  equal 0");    }    if (Nil == 0) {        NSLog(@"Nil  equal 0");    }        if (nil == 0) {        NSLog(@"nil equal 0");    }    NSLog(@"nil:%@ Nil:%@  NULL:%@",nil,Nil,NULL);        if (0 == [NSNull null] || nil == [NSNull null] ) {        NSLog(@"NSNull  equal 0");    }else{        NSLog(@"NSNull null is specially object represent null ");    }       NSLog(@"%@",[NSNull null]);
2014-11-01 15:17:27.141 AlwaysTest[70541:2600870] view can equal 0 2014-11-01 15:17:27.142 AlwaysTest[70541:2600870] NULL  equal 02014-11-01 15:17:27.142 AlwaysTest[70541:2600870] Nil  equal 02014-11-01 15:17:27.142 AlwaysTest[70541:2600870] nil equal 02014-11-01 15:17:27.142 AlwaysTest[70541:2600870] nil:(null) Nil:(null)  NULL:(null)2014-11-01 15:17:27.143 AlwaysTest[70541:2600870] NSNull null is specially object represent null 2014-11-01 15:17:27.143 AlwaysTest[70541:2600870] <null>

备注:

1.NSLog输出样子:nil/NULL为(null);[NSNull null]为<null>

2.nil/Null/nil跟零实质很接近,类型不同,编译器识别不同。

      判断零

  • 判断对象不存在,用==nil判断;判断对象“值为空”,用==[NSNull null](因为[NSNull null]总是返回一样的值,所以可以用==判断,"=="运算符判断数值);
  •  若一个对象不存在(a=nil 或者a=NULL),则用if(a==nil)或者if(a==NULL)或者if(!a)判断都为真;并且[a length]值为0;
  • 若一个对象为a=[NSNull null],则用if(a==nil)或者if(a==NULL)或者if(!a)判断都为假,并且[a  length]程序会crash; 
    NSString *element = [array objectAtIndex:2];  if ((NSNull *)element == [NSNull null]) {  //判断集合里元素是否为空,必须这种写法}  要判断数组元素是否为空,以下写法,都无效if(!element)if([element length]>0)if(element== NULL)if(element == Nil)

  • 判断字符串:用nil判断出对象存不存在,如果确定类型为字符串,length方法可以判断为不为空,如果不确定类型为字符串,需要考虑[NSNull  null]的情况
  • 从字典中取出一个对象,判断为空:用objectForKey与valueForKey取对象时有区别,此处不细说,注意objectForKey的定义: returns the value associated with aKey, or nil if no value is associated with aKey. 返回指定 key 的 value,若没有这个 key 返回 nil。所以如果确定字典中必含有这个key,则用[NSNull null]判断;如果字典中对象值为空时,未将key加到字典中,则用nil判断;

       json对象的值及类型判断

         待续……服务器传过来的‘空’值,null,在ios这边是什么?

         json中的空:将[NSNull null]存入字典,转换为json,则json为{"key":null};同样将json中的空值取出来时候,对象为[NSNull null]。


原创粉丝点击