IOS about nil and nil and the difference between null and <null>

Source: Internet
Author: User
Tags define null null null

The problem is this.

Nsdictionary *sample = [nsjsonserialization jsonobjectwithdata:received options:nsjsonreadingmutableleaves Error: &*messageinfo = [Sample objectforkey:@ "message"];

Sample is a dictionary, Messsageinfo is obtained from the dictionary according to the key value, and then through the log you can know that the value of Messageinfo is <null>.

At this point, if you need to determine whether its value is empty. Then you should do this:

For example, you need to pop up a message box.

if ([null]]) {        *alter = [[Uialertview alloc] Initwithtitle:@"Title" message:@ " incorrect number format, please re-enter " delegate: nil cancelbuttontitle:@ "OK"  Otherbuttontitles:nil];        [Alter show];    }
the difference between nil, NULL, nil, nsnull in iOS

1.nil
>defines The ID of a null instance.
Defines a null pointer to an object in OC that has an empty instance. is a object value.  
> Sample code:
NSString *somestring = nil;
Nsurl *someurl = nil;
ID someobject = nil;
if (Anotherobject = nil)//do something

> when an object is Rele ASE is also best to assign them to nil , which ensures security and, if not assigned nil, can cause the program to crash.
    nsarray * array = [Nsarray arraywithobjects:@ "test", @ "Test1", nil];
    [array release];
    *release should be assigned a value of nil

Otherwise the object will become a zombie object.
if (array)
{
Only an array of release, and no assignment to nil, somewhere in the program if you continue the array operation, the program directly crashes
NSString * string = [array objectatindex:0];
NSLog (@ "%@", string);
}

2 . NULL: is a general-purpose pointer (generic pointer)

>these macros define null values for classes and instances.
Null can be used on various pointers in the C language,
#define __DARWIN_NULL #define__DARWIN_NULLConstants

> Sample code:
int *pointertoint = NULL;
char *pointertochar = NULL;
struct TreeNode *rootnode = NULL;

> in Objective-c, the nil object is designed to be associated with a NULL null pointer. The difference is that nil is an object, and null is just a value. And we're not going to generate crash or throw exceptions for the nil call method.

   3.Nil
>defines the ID of a null class.
   defines an empty class, a null pointer to an OBJECTIVE-C class.
Available in Mac OS X v10.0 through Mac OS X v10.4.
Declared in NSObjCRuntime.h.
Declared Inobjc.h
> Sample code:
Class SomeClass = Nil;
Class Anotherclass = [NSString class];

   4.NSNull
>the NSNull class defines a singleton object used to represent null values in collection objects (which don ' t allow Nil Values).
The Nsnull class defines a singleton object used to represent the null value of a collection object

> Collection objects cannot contain nil as their specific value, such as Nsarray, Nsset, and Nsdictionary. Accordingly, the nil value is represented by a specific object nsnull. Nsnull provides a single instance for representing the nil value in the object's properties. The default implementation method, Dictionarywithvaluesforkeys: And setvaluesforkeyswithdictionary: automatically converts nsnull and nil to each other, so your object does not require nsnull test operations

IOS about nil and nil and the difference between null and <null>

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.