Adding swift files to the OC Project and implementing mixed programming

Source: Internet
Author: User
Tags naming convention uikit

For developers who are already working, it is not appropriate or conditional to start with a new Swift project. While the online tutorials on swift and OC mixed programming are much more, but mostly based on the SWIFT project, there is a certain discount on the reference value of our ocer.

There are still a lot of flaws in swift, but given the strength of Apple, it is time to give Swift a simple function in the project, giving itself, to the project, to swift a buffer, which is excellent.

Say so much, start on the code.


Now the background is: there is already a OC project, I want to join Swfit, and implement OC Invoke Swift, Swift calls OC.

1. Create a new Swift file

2. Next, you will be asked whether to set up a bridging file, click Yes, then I was redundant, so choose to cancel, we manually set up the bridge file

3. Create a bridging file manually: Create a new header file



About naming: Experienced people know that this header file is common to the project, and if created automatically, the file name is:

Project name-bridging-header.h

But because my current project name is Chinese, I named it myself.

4. Configure the header file

search for Swift in build setting and configure it according to the actual situation:

Note: Is relative to the relative path where the project file is located. Look inside the Finder, don't let the group in the project Disturb your judgment.


5. Introduce the required OC header file in the header file, I'll use the Uikit.


6. Writing code

As you can see, we can use the Uikit class.


Note: In the case of custom classes, you want a good naming convention, you will benefit from mixed programming.


7.oc Call Swift

Now because of the test I want to set the window's root controller to the one of my swift.

Because Swift does not have a header file, so it is difficult to do ...

In fact, the system defaults to the generation of the header file (our Swift class is the direct or indirect subclass of the NSObject), we just need to introduce

Project Name-swift.h

No way, my project name is Chinese ...

Introduced:


8. Classes that use Swift

9. Running

Everything is ok (ugly, don't throw up the groove)



Adding swift files to the OC Project and implementing mixed programming

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.