IOS Study Notes 2015-03-20 O nil, Nil, NULL, NSNull, 2015-03-20nsnull

Source: Internet
Author: User
Tags define null

IOS Study Notes 2015-03-20 O nil, Nil, NULL, NSNull, 2015-03-20nsnull

1. It is best to use nil [nil arbitrary method] for oc without crashing
Nil is an object value.
NULL is a generic pointer ).

2. NSNULL, NULL and nil should be essentially the same. NULL and nil are actually 0, but in Objective-c,
For a type like NSArray, nil or NULL cannot be used as the Object to be added to it. If an NSArray is defined, the memory is allocated to it, and the content of the NSArray is set to NULL,
You can use the object returned by [NSNULL null to initialize content in NSArray,
3. Because nil in NSArray and NSDictionary has a special meaning (indicating that the list ends), you cannot add the nil value in the set.
To store a value indicating "nothing", you can use the NSNull class. There is only one NSNull method:

+ (NSNull *) null;
Because the collection objects of Object-C, such as NSArray, NSDictionary, and NSSet, may all contain NSNull objects, if the item in the Code is NSNull, the program will crash.

 

//// Main. m // OC-Special Data Type /// 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 {// first introduce nil to indicate the object NULL pointer Person * p = [Person new]; NSLog (@ "% @", p = nil? @ "TRUE": @ "FALSE"); Person * p2; NSLog (@ "% @", p2 = nil? @ "TRUE": @ "FALSE"); Class pc = [Person class]; // describes whether Nil indicates whether the Class has NSLog (@ "% @", pc = Nil? @ "TRUE": @ "FALSE"); Class testClass; NSLog (@ "% @", testClass = Nil? @ "TRUE": @ "FALSE"); // introduces NULL as a value pair. The Objective-C value type is Null: int number = NULL; (equivalent to int number = 0;) (# define NULL (void *) 0) int * money = NULL; if (money = 0) NSLog (@ "money is NULL"); else NSLog (@ "money is not NULL "); // introduction NSNull null objects are mainly applicable to arrays because nil has special meanings in NSArray and NSDictionary (indicating that the list ends ), therefore, the nil value NSObject * obj1 = [[NSObject alloc] init]; NSObject * obj2 = [NSNull null]; NSObject * obj3 = [NSObjec T 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 coun T: 0 // an exception occurred! NSObject * obj1 = [NSNull null]; NSArray * arr1 = [NSArray arrayWithObjects: @ "One", @ "TWO", obj1, @ "three", nil]; for (NSString * str in arr1) {NSLog (@ "array object: % @", [str lowercaseString]);} // modify 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 ;}

 

Related Article

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.