Characteristics of OC Language (i)-message delivery and the representation of the calling function

Source: Internet
Author: User

When we first came to objective-c, we would think that the message passing in OBJC was similar to that of other languages, except that in OC, the method call was replaced by the concept of message passing.

So how do you distinguish between the message passing in OC and the calling function of other languages?

You can use the C language to compare with the OC language.

C Language Example

//declares a function that is used to get the larger value of two integer valuesintMaxintAintb); intMainintargcConst Char*argv[]) {    //call a function to get the maximum value of 4, 62 integer values    intresult = Max (4,6); //Print Resultsprintf"The larger value of 4 and 62 numbers is:%d", result); return 0;}

After compiling the run program, the compiler will prompt the following error

Undefined Symbols for Architecture x86_64:  ' _max ', referenced from:      _main in Main.old:symbol (s) not found for Architecture X86_64clang:error:linker command failed with exit code 1 (use-v to see invocation)

In the example above, we can realize that the language of the calling function after declaring the function, if there is no implementation function, the program can not be compiled through.

Then we'll see.

Examples of OC

#import <Foundation/Foundation.h>@interface  function:nsobject//  Declares a class method that is used to get the maximum value of two integer values + (int) Max: (int) v1:(int) v2; @end #import " Function.h " @implementation Function @end

Using only the build feature of the Xcode tool (COMMAND+B), we can see that the program can be compiled and passed, but there will be a yellow warning

 for ' Max:: ' not found!

The following crash message appears only when the program is running

+[function Max::]: Unrecognized selector sent toclass 0x1000010f8Terminating app due to uncaught exception'nsinvalidargumentexception', Reason:'+[function Max::]: Unrecognized selector sent to class 0x1000010f8'FirstThrowCall stack: (0Corefoundation0x00007fff8ea9925c__exceptionpreprocess +172    1LIBOBJC. A.dylib0x00007fff88555e75Objc_exception_throw + +    2Corefoundation0x00007fff8ea9c02d+[nsobject (NSObject) Doesnotrecognizeselector:] +205    3Corefoundation0x00007fff8e9f7272___forwarding___ +1010    4Corefoundation0x00007fff8e9f6df8_cf_forwarding_prep_0 + -    5Sendmethod0x0000000100000f24Main + the    6Libdyld.dylib0X00007FFF8A8F95FDStart +1) libc++abi.dylib:terminating with uncaught exception of type NSException

Through this form of comparison, I believe that you should be able to message the formal difference between the function and call can be seen.

Yes, the most important difference between the message passing and the calling function for programmers is whether the source code is compiled to pass through the process .

The principle of explaining the message passing mechanism is to use the runtime System (runtime)in the OC language.

The runtime system is a dynamic-link library that provides a range of public function interfaces and data structures that are located in/USR/INCLUDE/OBJC. Many of these functions allow you to use the pure C language to rewrite what the compiler does after you write the OC code . Other forms of interface are the methods defined in the NSObject class. These methods can be used to implement other run-time interfaces to improve operational efficiency. However, rewriting the runtime's code is not necessary for programming with the OC language, but a few run-time functions are useful for OC programs in special cases.

Next, let's take a look at these functions.

Objc_msgsend () Function Example

  

#import<Foundation/Foundation.h>@interfaceFunction:nsobject//declares a class method that is used to obtain the maximum value of two integer values+ (int) Max: (int) V1:(int) v2;@end#import "Function.h"@implementationFunction+ (int) Max: (int) V1:(int) v2{returnV1 > V2?V1:v2;}@end

Based on the function class above, the objc_msgsend () is used to complete the invocation of the OC method using the C-language functions .

    // gets the maximum value of 4, 62 integer values    int result = (intclass], @selector (max::),4,6);         // Print Results    NSLog (@ "4,6 Maximum value is:%d", result);

The message passing function provides all the necessary content for dynamic binding:

    • First, it finds the procedure (method implementation) called by the selector. Because the same method may have different implementations in different classes, this exact invocation process relies on the class that the receiver belongs to.
    • It then calls the procedure, passing the recipient object (a pointer to its data), and the parameters defined in the message.
    • Finally, it passes the return value of the procedure call as its own return value.
    • Note: The compiler automatically calls the message-passing function. You should not call the method directly in your own code.

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.