How can we make swift and OC compatible hybrid development in a project ?, Swiftoc

Source: Internet
Author: User

How can we make swift and OC compatible hybrid development in a project ?, Swiftoc

After Apple launched the swift language, many people worried that the OC would soon be replaced, but Apple said it would not abandon the OC within two years. But now it's faster. Some development teams have already started swift-based development, but many old frameworks have not yet been written into swift, and you do not want to write some swift functions. You want to write them using OC. Therefore, OC classes are used in swift programs from time to time. How can we seamlessly connect two languages in one program? In fact, it is very convenient

If you did not see this article in Dong Boran's blog Park, click to view the original article.

This is a newly created swift project, which is directly dragged into a SVProgressHUD framework.

Because files in the same project are stored in the same namespace in the swift language, some methods in the project do not need to import header files, which is indeed very convenient.

However, when you drag the OC framework and want to use the methods in the OC framework, an error will be reported when you import the header file of the framework to ViewController. swift.

Solution: Create a new Header File

 

After the framework is created, import the framework in this file.

 

Next, reference this file in the project deployment.

Imitating the path in the figure is the Head File in the folder of the project.

Cmd + B mutates. If success is used, it means that you have not written any mistakes.

 

Then, in the ViewController. swift file, use the swift format to call this method.

For example, write a click method (remember not to import the header file! You only need to write this method directly)

    override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {        SVProgressHUD.show()    }

 

Click after running

 

Now the bridge between OC and swift is completed. The HUD indicator framework has been seamlessly integrated with the swift project. If there are other OC classes in the future, the header file will be imported into this Demo-Bridge.h file, you can use the swift syntax (that is, the dot syntax) to call elsewhere in the project. Remember here. 1. Do not use OC syntax. 2. Do not import any header files in files suffixed with swift.

If you did not see this article in Dong Boran's blog Park, click to view the original article.

Of course, the Environment used in this article is the latest XCode6.3 beta2. both the simulator and SDK are iOS8.3. If the earlier version of Xcode encounters various problems during bridging, upgrade or download it to the latest version for development, because each version of swift has fine-tuned features.

 

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.