[Objective-C] send messages to nil

Source: Internet
Author: User
Tags scalar

Send messages to nil

In objective-C, sending messages to nil is completely valid-it does not work at runtime. Several Methods in cocoa have taken advantage of this. The returned values of messages sent to nil can also be valid:
• If a method returns an object, the message sent to nil returns 0 (nil ). For example: person * motherinlaw = [aperson spouse] Mother]; if the spouse object is nil, the message mother sent to nil will also return nil.
• If the return value of a method is of the pointer type, the pointer size is an integer scalar that is smaller than or equal to sizeof (void *), float, double, long double, or long, the message sent to nil will return 0.
• If the return value of a method is a struct, as in the Mac OS X Abi function call guide, the message sent to nil will return 0. The values of each field in the struct will be 0. Other struct data types are not filled with 0.
• If the return value of the method is not in the preceding situations, the return value of the message sent to nil is undefined.
The followingCodeSection is an example of effectively sending messages to nil:

[CPP] View
Plaincopy

  1. ID anobjectmybenil = nil;
  2. // This method is valid.
  3. If ([Anobjectmaybenil methordthatreturnadouble] == 0.0)
  4. {
  5. // Other implementation code
  6. }

note : in Mac OS X v10.5, the message sending result to nil is slightly different from the above description. In Mac OS X v10.4 and later versions, sending messages to nil is completely valid, as long as the returned message value is an object, any type of pointer, void, or other integer scalar values smaller than or equal to sizeof (void. At this time, the message sent to nil will return nil. If the return value of the message sending NIL is not of the above types (for example, the returned type is struct, floating point, or vector type), the return value is undefined. Therefore, in MAC
OS X v10.4 and earlier versions, we should not rely on the return values of messages sent to the NIL object unless the return value of the message is an object, pointer of any type, or integer scalar of any size smaller than or equal to sizeof (void.

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.