IOS Learning Note 2015-03-20 O's nil,nil,null,nsnull

Source: Internet
Author: User

1.oc best with nil [nil arbitrary method], will not crash
Nil is an object value.
Null is a general-purpose pointer (generic pointer).

2. Nsnull,null and nil are essentially the same, and null and nil are actually 0, but in Objective-c,
For a type like nsarray, nil or null cannot be an object added to it, if you define a Nsarray, allocate memory for it, and then set the contents to be empty,
You can initialize the contents of Nsarray with [NSNULL null returned object.
3. Because there is a special meaning in nil in Nsarray and nsdictionary (indicating the end of the list), you cannot put a nil value in the collection.
If you really need to store a value that says "Nothing", you can use the Nsnull class. There is only one way to Nsnull:

+ (NSNull *) null;
Because Object-c collection objects, such as Nsarray, Nsdictionary, Nsset, and so on, can contain Nsnull objects, it can cause the program to crash if item in the code is nsnull.

////main.m//oc-Special Data types////Created by Wangtouwang on 15/3/20.//Copyright (c) 2015 Wangtouwang. All rights reserved.//#import<Foundation/Foundation.h>#import "Person.h"intMainintargcConst Char*argv[]) {@autoreleasepool {//First, nil represents the object null pointerPerson *p = [personNew]; NSLog (@"%@", P==nil?@"TRUE":@"FALSE"); person*P2; NSLog (@"%@", P2==nil?@"TRUE":@"FALSE"); Class PC= [Personclass]; //describes whether Nil represents the existence of a classNSLog (@"%@", Pc==nil?@"TRUE":@"FALSE");        Class TestClass; NSLog (@"%@", Testclass==nil?@"TRUE":@"FALSE"); //describes null NULL is a value pair objective-c value type is null: int number = NULL; (equivalent to int # = 0;) (#define NULL ((void *) 0))        int*money=NULL; if(Money = =0) NSLog (@"Money is NULL"); ElseNSLog (@"Money was not NULL"); //Introduction NSNull empty objects are primarily applicable to arrays because there is a special meaning in nil in Nsarray and nsdictionary (indicating the end of the list), so you cannot put a nil value in the collectionNSObject *obj1 =[[NSObject alloc] init]; NSObject*obj2 = [NSNullNULL]; NSObject*OBJ3 = [NSObjectNew]; NSObject*Obj4; Nsarray*ARR1 =[Nsarray arraywithobjects:obj1, Obj2, Obj3, Obj4, nil]; NSLog (@"arr1 count:%ld", [arr1 Count]);//arr1 Count:3NSObject*obj1; NSObject*obj2 =[[NSObject alloc] init]; NSObject*OBJ3 = [NSNullNULL]; NSObject*OBJ4 = [NSObjectNew]; Nsarray*ARR2 =[Nsarray arraywithobjects:obj1, Obj2, Obj3, Obj4, nil]; NSLog (@"arr2 Count:%ld", [arr2 Count]);//arr2 count:0//There are exceptions! NSObject *obj1 = [NSNullNULL]; Nsarray*ARR1 = [Nsarray arraywithobjects:@" One",@" Both", Obj1,@"three", nil];  for(NSString *strincharr1) {NSLog (@"Array object:%@", [str lowercasestring]); }                //ModifyNSObject *obj1 = [NSNullNULL]; Nsarray*ARR1 = [Nsarray arraywithobjects:@" One",@" Both", Obj1,@"three", nil];  for(NSString *strincharr1) {            if(! [Str isequal:[nsnullNULL]]) {NSLog (@"Array object:%@", [str lowercasestring]); }        }            }    return 0;}

IOS Learning Note 2015-03-20 O's nil,nil,null,nsnull

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.