IOS 學習筆記 2015-03-20 O之 nil,Nil,NULL,NSNull,2015-03-20nsnull

來源:互聯網
上載者:User

IOS 學習筆記 2015-03-20 O之 nil,Nil,NULL,NSNull,2015-03-20nsnull

 1.oc最好 用nil   [ nil  任意方法],不會崩潰
 nil 是一個對象值。
NULL是一個通用指標(泛型指標)。

2. NSNULL,NULL和nil在本質上應該是一樣的,NULL和nil其實就是0,但是在Objective-c中,
   對於像NSArray這樣的類型,nil或NULL不能做為加到其中的Object,如果定義了一個NSArray,為其分配了記憶體,又想設定其中的內容為空白,
   則可以用[NSNULL null返回的對對象來初始化NSArray中的內容,
3.因為在NSArray和NSDictionary中nil中有特殊的含義(表示列表結束),所以不能在集合中放入nil值。
  如要確實需要儲存一個表示“什麼都沒有”的值,可以使用NSNull類。NSNull只有一個方法:

+ (NSNull *) null;
因為Object-C的集合對象,如NSArray、NSDictionary、NSSet等,都有可能包含NSNull對象,所以,如果一下代碼中的item為NSNull,則會引起程式崩潰。

 

////  main.m//  OC-特殊資料類型////  Created by wangtouwang on 15/3/20.//  Copyright (c) 2015年 wangtouwang. All rights reserved.//#import <Foundation/Foundation.h>#import "Person.h"int main(int argc, const char * argv[]) {    @autoreleasepool {        // 首先介紹nil 代表對象null 指標        Person  *p = [Person new];        NSLog(@"%@",p==nil?@"TRUE":@"FALSE");        Person *p2 ;        NSLog(@"%@",p2==nil?@"TRUE":@"FALSE");                Class pc = [Person class];        // 介紹 Nil 代表類是否存在        NSLog(@"%@",pc==Nil?@"TRUE":@"FALSE");        Class testClass ;        NSLog(@"%@",testClass==Nil?@"TRUE":@"FALSE");                //介紹 NULL NULL 是個值  對Objective-C實值型別為空白:int number = Null;(相當於 int number = 0;)  ( #define NULL ((void *)0)  )        int *money=NULL;        if (money == 0 )            NSLog(@"money is NULL");        else            NSLog(@"money is not NULL");                //介紹 NSNull Null 物件 主要是適用於 數組  因為在NSArray和NSDictionary中nil中有特殊的含義(表示列表結束),所以不能在集合中放入nil值        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                        //有異常!        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 isEqual:[NSNull null]]){                NSLog(@"array object: %@", [str lowercaseString]);            }        }            }    return 0;}

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.