Nil, Nil, NULL, and NSNull in Objective C. nilnsnull

Source: Internet
Author: User
Tags define null

Nil, Nil, NULL, and NSNull in Objective C. nilnsnull

Kenyo's original statement is: the estimation of IOS development is a headache for Objective-C's memory management mechanism. Accidentally, the program will leak memory, and I am no exception, A few days ago, the nil and release of the pointer were miserable. I want to explain in detail the differences and usage of nil and release in Objective-C.

First, let's talk about the role of both of them. nil is to set the pointer of an object to null, but cut off the connection between the pointer and the object in the memory. It has no effect on memory release; release is actually used for memory release. after release, the system will mark the block of memory as available (reallocable ). Therefore, nil does not release the memory. Only release actually releases the memory.

The use order of the two methods. If there is no release, nil is used directly. Although there is no error (a null pointer of release is valid), it means that it creates a memory leak, because nil does not work after release, my previous lesson is that I accidentally put nil before release, so leak always reports Memory leakage.

On the contrary, if you set nil after release, this problem will not occur, but some people will ask, what will happen if release is not set nil? In fact, the program may not report errors, but you need to know that nil is set to prevent pointer disorder, because the memory allocated to an object after release has been released, if the pointer is not left blank after release, and the system misuse the pointer, the program will crash (this situation is particularly prone to the function of delayed call ), if the pointer is set to null after it is released, the system will not crash even if the subsequent program uses the pointer. Therefore, when Objective-C releases the memory, it must first release and then nil.

Another point is that some readers may often see the code self in the dealloc function in my tutorial. xxx = nil; now, do you understand what it is? It is equivalent to [xxx release]; xxx = [nil retain]; (--- if your property (nonatomic, retian) xxx, then this will happen. If not, right ). This means that the class variables after the @ property operator are used will generate the set and get methods, and the specific Writing of the set method is as follows:

 

-(Void) setNames :( NSArray *) names {
NSLog (@ "setNames ");
If (_ name! = Name ){
[_ Name release];
_ Name = [name retain];}
}
When the original variable is compared with nil, it is automatically released if it is not equal to, and then xxx = [nil retain] is executed;

 

Nil
  • Nil is the literal null value of the ObjC object, corresponding to the id type object, or the ObjC object declared by @ interface.
  • For example:
    NSString *someString = nil;NSURL *someURL = nil;id someObject = nil; if (anotherObject == nil) // do something
  • Definition:
    // objc.h#ifndef nil# if __has_feature(cxx_nullptr)#   define nil nullptr# else#   define nil __DARWIN_NULL# endif#endif // __DARWIN_NULL in _types.h #define __DARWIN_NULL ((void *)0)

 

Nil
  • Nil is a written null value of the ObjC Class type, which corresponds to a Class object.
  • For example:
    Class someClass = Nil;Class anotherClass = [NSString class];
  • The definition Definition is similar to that of nil, with the same value:
    // objc.h#ifndef Nil# if __has_feature(cxx_nullptr)#   define Nil nullptr# else#   define Nil __DARWIN_NULL# endif#endif
NULL
  • NULL is a NULL value of any C pointer.
  • For example:
    int *pointerToInt = NULL;char *pointerToChar = NULL;struct TreeNode *rootNode = NULL;
  • Definition:
    // in stddef.h #define NULL ((void*)0)
NSNull
  • NSNull is a class that represents null values and is an ObjC object. In fact, it only has one Singleton method: + [NSNull null], which is generally used to indicate objects with null values in the set.
  • Example:
    // Nil cannot be stored in the Foundation set because nil is used to end the set.NSArray *array = [NSArray arrayWithObjects:@"one", @"two", nil]; // Incorrect useNSMutableDictionary *dict = [NSMutableDictionary dictionary];[dict setObject:nil forKey:@"someKey"]; // Correct useNSMutableDictionary *dict = [NSMutableDictionary dictionary];[dict setObject:[NSNull null] forKey:@"someKey"];
  • Definition:
    /*  NSNull.h    Copyright (c) 1994-2012, Apple Inc. All rights reserved.*/ #import <Foundation/NSObject.h> @interface NSNull : NSObject <NSCopying, NSSecureCoding> + (NSNull *)null; @end
NIL or NSNil

ObjC does not exist!

Summary

Although the values of nil, Nil, and NULL are the same, it is important to understand the written meaning between them, so that the code is clearer and more readable.

References
  • Difference between nil, NIL and null-Stack Overflow
  • Topics for Cocoa: Using Null.
BOOL value and bool value in object c

Typedef signed char BOOL;

# Define YES (BOOL) 1

# Define NO (BOOL) 0

From the preceding definition, we find that the value of the Boolean variable is YES/NO, or 1/0. YES or 1 indicates true, and NO or 0 indicates false. For example, you have defined a Boolean variable and assigned a value:

BOOL enabled = NO;
Enabled = 0;

Determine whether the BOOL value is YES:

If (enabled = YES ){

}

Or YES can be omitted

If (enabled ){

}

Determine whether the BOOL value is NO:

If (! Enabled ){

}

Or

If (enabled! = YES ){

}

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.