When doing the project today, in the array inside the value, found that there are nsnull objects, and then use the corresponding object in the array to assign values when there are various problems, always error. After studying and checking the data, finally solved the problem.
Nil is used to assign values to an object (any object in objective-c is of type ID), NULL assigns any pointer, null and nil are not interchangeable, nil is used for class pointer assignment (in OBJECTIVE-C, the class is an object, is an instance of the Meta-class of a class), whereas Nsnull is used for collection operations, although they represent null values, they are used in completely different situations.
1, Nil: general assignment to empty objects;
2, NULL: general assignment to a null value other than nil. such as SEL, etc.;
[Nsapp Beginsheet:sheet
Modalforwindow:mainwindow
Modaldelegate:nil//pointing to an object
Didendselector:null//pointing to a non object/class
Contextinfo:null]; Pointing to a non object/class
3, Nsnull:nsnull only one method: + (NSNULL *) null;
[NSNull NULL] is used to add a non-nil (representing the end of the list) to a null value in Nsarray and Nsdictionary. [NSNull NULL] is an object that he uses in situations where nil can not be used.
4, when sending messages to nil, return no, there will be no exception, the program will continue to execute;
You receive an exception when you send a message to an Nsnull object.
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. NSNull has only one method: + (NSNull *) null.
When I was doing the project today, I had to judge if the array element is empty, and my following notation is invalid if (!element) if ([element length]>0) if (element== NULL) if (element = = Nil) The correct way to determine whether an array and a dictionary element is empty is:
Determines whether the inside element of an array object is empty
NSString *element = [array objectatindex:2];
if ((NSNull *) Element = = [NSNull null]) {
}
Determines whether the inside element of the Dictionary object is empty
Nsdictionary *dictionary = [nsdictionary Dictionarywithobjectsandkeys: @ "IPhone" @ "First" @ "IPad" @ "Second", nil];
- NSString *value = [Dictionary objectforkey:@ "First"];
- if ((NSNull *) value = = [NSNull null]) {
- }
The use of iOS nil,null,nsnull