A detailed explanation of the swift3.0 and OC

Source: Internet
Author: User

Objective:

In some cases, it is not possible to complete a project using Swift alone, and it is necessary to use OC to implement some functions in the Swift project, for example, the project will use some third-party frameworks, but this third-party framework is implemented in OC, or your project must invoke some of the functionality of the original project, The original project was also implemented with OC. In such a context, you have to realize the mixing of the two languages. The reverse is the same, for example, the developer intends to use Swift to implement some new features to use the three-way framework, and others are written by Swift, and your project is really OC, this time you have to implement a mixed. Let's illustrate some of them in two different ways.

One: Mixed in the Swift project

The following content we strive to speak of the popular point, in fact, it is not complicated.

In a swift project, you add a new file, but this file may be of OC type. That programming language you will certainly choose OC, the creation of the file, remember, is not a burden.

First time, remember is the first time will have the following picture shows a hint;

There are a lot of blog on the Web, I now use Xcode is the latest 7.3, choose "Create Bridging Header" on OK. Look at the following picture again;

I directly is the interface of Xcode, you can see a I selected this file, the role of this file you want to use in the swift file of the OC file, you will add OC header file to this newly added file, for example, I put OctestViewController.h added to this file. You can then use this OC-type file directly in the Swift file, but the syntax must be Swift's syntax.

What if you want to use Swift type files in OCTESTVIEWCONTROLLER.M? You first search for a swift in your project's Build setting to see the picture;

Look at the line I've chosen. Swift_text-swift.h you want to add the Swift type file to the OC file, you can import the header file into your OC file and it's OK. Of course, although you call the swift type of file, but you are in the OC file written, the syntax is definitely the OC syntax!

Two: Mixed in OC project

In fact, after my practice, I found the exact same. You add the swift type file to the OC project and a bridgehead file is generated. You want to use the Swift type file in the OC file, also the same: In the build setting Swift head file into your OC file, you want to use the OC type file in Swift file, also the same as the OC type file header file into the bridgehead file is OK, by the way, I saw in some books, said in the Swift class It is not correct to define a class that is called @Objc before being called by the OC class, which may have been before, but not required in swift3.0! Well, you know.

Detailed swift3.0 and OC's mixed

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.