An explanation of inline functions of IOS OC Inline

Source: Internet
Author: User

Inline

In some frameworks in iOS, static inline is a keyword combination that often occurs.

    • Static does not have to say more, indicating that it is applied in the current file, such as static A, and that static a can appear in other files. Errors that do not cause duplicate names.
    • inline. Inline functions.
      • Role: Replace macro. (If you look at the frame, see the inline puzzled, search this article, see here can not see the following details)
      • When inline is present in the frame, such as the yykit frame. We can see that it appears in the. h file with a little observation. such as:
static  Inline cgfloat cgfloatfrompixel ( cgfloat value) {return Value/yyscreenscale ();} //yyscreenscale () method Description: cgfloat Yyscreenscale () {static cgfloat scale; static dispatch_once_t Oncetoken; dispatch_once (&oncetoken, ^{scale = [UIScreen Mainscreen].scale;}); return scale;}            

Although the static inline modifier is a function (or method, Swift comes out, I think the method = = function, friends do not have to use the words, despise my generation). But it is here that you can use Cgfloatfrompixel as a macro.
Of course, the inline function differs from a macro, and inline can:

    • To solve the problem of function call efficiency:
      • The call between functions is a call between memory addresses and returns the address of the original function when the function call is complete. function calls have time overhead, and inline functions are meant to solve this problem.
      • Without inline-modified functions, the call command appears in the assembly. Calling the Invoke command is required:
        • (1) Place the address of the next instruction in the stack
        • (2) and the starting address of the subroutine into the PC (so that the next instruction of the CPU will be transferred to execute the subroutine).
Conclusion why can inline replace macros?
    • Advantages compared to functions:

      • 1) The inline function avoids the common function, in the assembly must invoke the disadvantage of call: Cancel the function of the parameter stack, reduce the cost of the call, improve efficiency. Therefore, the execution speed is faster than the normal function.

      • 2) Integration of the advantages of the macro, the use of direct replacement with the code (like a macro);

    • Advantages compared to macro:

      • 1) Avoid the disadvantages of macros: Pre-compilation is required. Because inline inline functions are also functions, you do not need to precompile.

      • 2) When an inline function is called, the compiler first checks the type of its arguments to ensure that the call is correct. Then perform a series of related checks, just like any real function. This eliminates its hidden dangers and limitations.

      • 3) You can use the protected members and private members of your class.

Description of inline inline functions
    • 1. The inline function is just the application we provide to the compiler, and the compiler does not necessarily take the inline form to invoke the function.
    • 2. Inline functions cannot host large amounts of code. If the function body of the inline function is too large, the compiler automatically discards the inline.
    • 3. Loop statements or switch statements are not allowed in inline functions.
    • 4. The definition of an inline function must precede the call.
Resources:

Http://stackoverflow.com/questions/11985307/static-extern-and-inline-in-objective-c

http://tigcc.ticalc.org/doc/gnuexts.html#SEC93



Wen/Fan Lu (Jane book author)
Original link: http://www.jianshu.com/p/d557b0831c6a
Copyright belongs to the author, please contact the author to obtain authorization, and Mark "book author".

An explanation of inline functions of IOS OC Inline

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.