Swift 4 and Objective-c hybrid programming (i) fast start

Source: Internet
Author: User

methods of mixing and programming of Swift 4 and objective-c in the same project


You can use both Swift and objective-c( OC) in Xcode to write code, and the benefits of mashup programming are many, such as allowing a lot of code to be reused, Find a balance between performance and development efficiency.


refer to OC in Swift

When we build a project,XCode asks what language we choose to develop, and if you choose OC, the development environment will ask you to create one when you create a Swift file for the first time . H file. This . h file is named "#ProjectName #-bridging-header.h" . You can let the development environment help you create this file, you can build it yourself, the compiler only recognize this file name, as long as not to write wrong.


We call this file a bridging file, which is a function of selectively exposing the OC code to Swift, allowing Swift to invoke these interfaces.

The way you expose it is through import, which means you can import any code you want to expose to Swift in this . h file, and then you can use the Swift Accessed in the.


Try the following steps:

  1. Open Xcodeand create an OC project HelloWorld

  2. Create a call Hellooc OC class, implement static method Hello, implement code NSLog ("Hello, OC.")

  3. Create a call Helloswift Swift class, inherits NSObject, implements static function Hello (), implements code NSLog ("Hello, Swift.")

  4. at this time Xcode will pop up to ask whether to create a file helloworld-bridging-header.h , choose Yes, if you click No, you create a

  5. in the Helloworld-bridging-header.h code, add #import "HelloOC.h"

  6. in the Helloswift in the hello function, in NSLog ("Hello, Swift.") Append after Hellooc.hello ()


If the compilation succeeds, it means that the compiler allows you to call Hellooc 's code through helloswift .

The execution of the program at this time will find that the output terminal does not print anything. Because the program principal itself does not call Helloswift, we are building the OC Project, so we need OC to invoke the Swift code.


quoting Swift in OC

When we set up helloswift ,Xcode actually did some backstage work, in addition to asking if you establish "Helloworld-bridging-header.h" outside, It also implicitly creates a header file called "Helloworld-swift.h" , remember that this file is created by Xcode implicitly, so do not create this file yourself, Many people find that it is necessary for this header file to be created without looking carefully, resulting in various compilations that do not pass.

This helloworld-swift file from file to code is Xocode dynamically generated, you do not need to edit it, if you are interested in exactly what is written, you can import this file , jump toDefinition of the way to explore.


when you need to expose the Swift class to OC calls, you do not need to import "Helloworld-swift.h" by any means of import. Can.

Try the following steps:

    1. in the viewcontroller.m file, introduce the header file #import "Helloworld-swift.h"

    2. in the implementation of the Viewdidload method, invoke [Helloswift hello];


At this time compile, execute, you can find the project print log output as:

Hello, Swift.

Hello, OC.


So far, we've simply called the Swift code through the OC code and called OC through the Swift code. But this is just the most basic case of mutual invocation. There are more complex special cases in the actual project, which need to be solved by other techniques (such as naming incompatibility problems, etc.). These are left behind to continue writing.




Reference:https://developer.apple.com/library/content/documentation/Swift/Conceptual/BuildingCocoaApps/ Mixandmatch.html

---

Friends who want to exchange development technology can add group:527628370


This article is from the "Smart Coffee House" blog, please be sure to keep this source http://dothebest.blog.51cto.com/699987/1939590

Swift 4 and Objective-c hybrid programming (i) fast start

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.