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.
Examples are as follows:
- ID object = nil;
- Judge object is not empty
- if (object) {
- }
- Null to determine Object
- if (object = = nil) {
- }
- Array initialization, NULL value end
- Nsarray *array = [[Nsarray alloc] initwithobjects:@ "First", @ "Second", nil];
- Determines whether an array element is empty
- NSString *element = [array objectatindex:2];
- if ((NSNull *) Element = = [NSNull null]) {
- }
Today when the project was encountered, to determine whether the array element is empty, I have the following notation, if (!element) if ([element length]>0) if (element== NULL) if (element = = Nil)
- Determines whether the element of a Dictionary object is empty
- Nsdictionary *dictionary = [Nsdictionary Dictionarywithobjectsandkeys:
- @ "IPhone" @ "First" @ "IPad" @ "Second", nil];
- NSString *value = [Dictionary objectforkey:@ "First"];
- if ((NSNull *) value = = [NSNull null]) {
- }
You are welcome to continue to add their differences. Deepen understanding of a
1, Nil: general assignment to empty objects;
2, NULL: general assignment to a null value other than nil. such as SEL, etc.;
Give me a chestnut (good weight ~):
[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. There is only one way to Nsnull:
+ (NSNull *) null;
nil是一个对象指针为空,Nil是一个类指针为空,NULL是基本数据类型为空。这些可以理解为nil,Nil, NULL的区别吧。
Deepen understanding of the second
On the difference between nil and null and its related problems
1, nil and null from the literal meaning to understand the simpler, nil is an object, and null is a value, I understand that nil is to set the object to NULL, and NULL is the basic type is set to empty, the personal feel a bit like the attribute, the basic type is assigned to assign NSString types are generally assigned copy, whereas objects are generally retain. And we're not going to generate crash or throw exceptions for the nil call method.
Look at some
Nil-Null-pointer to Objective-c object
NIL-Null-pointer to Objective-c class
null-> null pointer to primitive type or absence of data.
Take a look at the usage
Nsurl *url = nil;
Class class = Nil;
int *pointerint = NULL;
Nil is an object pointer is empty, nil is a class pointer is empty, NULL is the basic data type is empty. These can be understood as nil,nil, the difference between null.
2, one can study the problem
In the Dealloc
-(void) dealloc
{
Self.test = nil;
[_test release];
Test = nil;
}
The difference between the few
First, the simplest [_test release]; This is to reduce the reference technology by 1, the so-called reference count is to see that there are multiple pointers to a piece of memory entity, when the release of the pointer is reduced by one, release to 0, it is really the memory of the return to the system of the time
Again self.test = nil; it's easy to understand the attributes and setter and Getter methods.
-(void) Settest: (NSString *) newstring
{
if (_test! = newstring)
[_test release];
_test = [newstring retain];
}
-(NSString *) test
{
return _test;
}
This is the setter and getter method, and in this problem the equivalent of the code changes to
if (_test! = nil)
[_test release];
_test = nil;
Now it is easier to explain that the setter method will retain the Nil object, before this has been the release of the old object, the advantage is that the member variable is not the opportunity to point to random data, and in other ways, there may be a situation pointing to random data. When the release is in place, if there is another way to access it, if it is already dealloc, it may be crash, and when it points to nil, no error will occur. Nil is the counter is 0, so to speak, when the real release of an object, NSLog is not print it points to the memory control, and when nil, it can be printed out to point to a memory space.
So now it's not difficult to explain test = nil; The simple use of this can be said to create their own memory leaks, it can be understood, is equivalent to the pointer to the object directly and the object clean break. The test points directly to nil, and the memory entities do not disappear, and there is no system reclamation.
< >ios nil, NULL, and Nsnull use