Null vs nil in objective-C

Source: Internet
Author: User

Nil can only be applied in the ID type, that is, the pointer to the object in Java and C ++. Null is used as a non-object pointer.

See the following example:

 -(Void)Observevalueforkeypath Nsstring *)Keypath ofobject ID)Object

 
Change:(Nsdictionary *)Change Context:(Void *)Context
The context parameter is a null pointer type and a C-type pointer. Therefore, it is defined as null. The NULL pointer type is sometimes declared
(Void *) 0, rather than ID-type nil.

Original article:

"Nil" shocould only be used in place of an "ID", what we Java and C ++
Programmers wocould think of as a pointer to an object. Use NULL
Non-object pointers.

Look at the Declaration of that method:

-(Void)Observevalueforkeypath:(Nsstring *)Keypath ofobject:(ID)Object
Change:(Nsdictionary *)Change Context:(Void *)Context

Context is a "Void *" (ie a C-style pointer), so you 'd definitely use
NULL (which is sometimes declared as "(void *) 0") rather than Nil
(Which is of type "ID ").

 

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.