Nil is used to assign values to objects (any object of object-c is of type ID), NULL assigns any pointer, null and nil are not interchangeable, nil is used for class pointer assignment (in Object-c, class is an object, is an instance of class Meta-class), The nsnull is used for collection operations, although they represent null values, but the use of the situation is completely different, so at the time of encoding strictly according to the variable type to assign values, the correct null value to the correct type, so that the code is easy to read and maintain, it is not easy to cause errors. 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 types like nsarray, nil or null cannot be an object added to it, If you define a Nsarray, allocate memory for it, and want to set the contents to be empty, you can initialize the content in Nsarray with [NSNULL null], 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.
NSString *item=[nsarray objectatindex:i];if ([item isequaltostring:@ "Testnumber"]) { //} The following code is a common error, The release object is not set to nil, which causes the program to crash. ID someobject=[[object alloc] init];//... [Someobject release];//...if (someobject) { //crash here}
Nil is used to assign values to objects (any object of object-c is of type ID), NULL assigns any pointer, null and nil are not interchangeable, nil is used for class pointer assignment (in Object-c, class is an object, is an instance of class Meta-class), The nsnull is used for collection operations, although they represent null values, but the use of the situation is completely different, so at the time of encoding strictly according to the variable type to assign values, the correct null value to the correct type, so that the code is easy to read and maintain, it is not easy to cause errors.
The Judge object is not empty if (object) {}//The Judgment object is null if (object = = nil) {}//array initialized, NULL end Nsarray *pagenames=[[nsarray alloc] initwithobjects:@ " Documentlist ", @" AdvancedSearch ", @" Statistics ", nil];//determine if the array element is empty Uiviewcontroller *controller=[nsarray Objectatindex:i];if ((NSNull *) controller = = [NSNull null]) { //}//determines whether the element of the Dictionary object is empty nsstring *userid=[nsdictionary objectforkey:@ "userid"];if (userid = = [NSNull null]) { //}
Object-c has a lovely feature, that is, when sending a message to a nil object, the system returns a value of 0 instead of causing an exception, which is completely different from Java's annoying nullpointerexception and the process of a C/s program that crashes directly. Understand that this feature of OBJECT-C is important for developing the correct iOS program because nil is the legal value of the object, and the nil object can also receive messages such as:
Person=nil; [Person Castballot]; NSLog ("person=%@", person);
Summary of Objective-c Nil,nil,null and Nsnull