Use of IOS nil, null, and nsnull

Source: Internet
Author: User

Nil is used to assign values to an object (any object in objective-C belongs to the ID type). null is used to assign values to any pointer. null and nil cannot be exchanged, nil is used to assign values to class pointers (in objective-C, a class is an object and an instance of class meta-class), while nsnull is used for set operations, although they indicate null values, however, the usage is completely different.

Example:

  1. Id object = nil;
  2. // The Judgment object is not empty.
  3. If (object ){
  4. }
  5. // The Judgment object is empty.
  6. If (Object = nil ){
  7. }
  8. // Array initialization. The Null Value ends.
  9. Nsarray * array = [[nsarray alloc] initwithobjects: @ "first", @ "second", nil];
  10. // Determine whether the array element is empty
  11. Nsstring * element = [array objectatindex: 2];
  12. If (nsnull *) element = [nsnull null]) {
  13. }
  14. // Determine whether the element of the dictionary object is null
  15. Nsdictionary * dictionary = [nsdictionary dictionarywithobjectsandkeys:
  16. @ "IPhone", @ "first", @ "iPad", @ "second", nil];
  17. Nsstring * value = [dictionary objectforkey: @ "first"];
  18. If (nsnull *) value = [nsnull null]) {
  19. }
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.