Simultaneous use of objective-c and Swift in the project

Source: Internet
Author: User

The Swift language released by Apple can exist in one project with the previous objective-c language.

There may be people who think that there can be objective-c or swift in the same class file, which is not right. In the same class file or in the same code block, you can not have both code for both languages, but separate Swift class files or objective-c class files can be called between each other.

Let's talk about how to refer to the Swift class in the Objective-c class and the specific steps for referencing the Objective-c class in the Swift class.

Using Objective-c in Swift

Start by creating a swift iOS project:

Add a objective-c class file, this class is as follows:

While adding the Objective-c file, Xcode prompts you to:

Click Yes!

Use the Objective-c class that you just added in VIEWCONTROLLER.M:

To use Swift in Objective-c, the steps are similar:

Start by creating a objective-c iOS project:

To add a swift class file:

This class is as follows:

Add in Yourproject-bridging-header.h:

Use the Swift class that you just added in VIEWCONTROLLER.M:

Reference Document: Http://stackoverflow.com/questions/24002369/how-to-call-objective-c-code-from-swift

Simultaneous use of objective-c and Swift in the project

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.