ios-swift, objective-c, C + + mixed programming

Source: Internet
Author: User
<span id="Label3"></p><p><p>1. Objective-c calling C + + code</p></p><p><p>The suffix of M file is the executable file of objective-c, and the objective-c++ file is a mm File.</p></p><p><p>C + + code cannot be called directly in objective-c, so if you need to call the C + + language in objective-c you need to change the suffix m file directly to mm, then you can call C + + Code.</p></p><p><p>OBJECTIVE-C compatible c,objective-c++ compatible with c, C + +.</p></p><p><p></p></p><p><p>The next step is to create a C + + file in the OC project and invoke the C + + code:</p></p><p><p></p></p><p><p>C + + is then invoked directly in the OC file using C + + syntax, so the OC language execution file suffix is mm file.</p></p><p><p></p></p><p><p>2. C + + call Objective-c code</p></p><p><p>Start by creating a new Objective-c class in the project used above, and add a simple instance Method.</p></p><p><p></p></p><p><p>But it is not directly in the C + + file directly call OC code, will directly fail to error, because C + + introduces the OC class header file, which is not through the OC syntax, So in OC the. h header file is not able to have any OC syntax, so sorry to remove the code from the above oc. h header file and change the oc. m file suffix to. mm file:</p></p><p><p></p></p><p><p>Then call the OBJECTIVE-C code in the C + + code:</p></p><p><p></p></p><p><p>We then create the Objective-c class and invoke it in a specific way:</p></p><p><p></p></p><p><p>Note an error-prone detail:</p></p><p><p>If a C + + method call is created after an object or method, an error will be made, the object or method is not declared and defined, so it must be placed behind to perform the pass. Note here that the syntax for an OC object invocation method is: [instance Object method].</p></p><p><p></p></p><p><p>3. Swift calls C language code</p></p><p><p>This and Swift call C + + language code is the same, in the SWIFT project to create a new C + + or C language files, will automatically generate a bridge file, write good C + + code or C code, remember to import its header file into this bridge File. You can then directly use the methods of C + + and C directly with Swift syntax in Swift Files.</p></p><p><p>4, Swift, objective-c, C + + mixed programming</p></p><p><p>One thing to emphasize here: Swift is not directly using C + + code, But you can create a objective-c file as an intermediary to invoke C + + Code. But sometimes it's better not to do this when designing a program, because the process of Swift, C + +, objective-c, takes a long time. If it can be achieved through C language, it is best to use C language directly.</p></p><p><p>There are a lot of useful libraries written in C + +, so if Swift needs to use C + + libraries, it is necessary.</p></p><p><p>Concrete practice oneself experience it separately.</p></p><p><p></p></p><p><p></p></p><p><p></p></p><p><p></p></p><p><p></p></p><p><p>ios-swift, objective-c, C + + mixed programming</p></p></span>

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.