Swift call objective-c code for iOS development

Source: Internet
Author: User

This article was written in 2014.09.25

The recent iOS development new programming language Swift has a large response in the iOS development circle, there are many tutorials or small samples at home and abroad.
While the language of Swift is still evolving, and the changes are still big, Apple is not promising that the code currently being written will be compatible in the future, but still cannot resist the enthusiasm of iOS developers.

Why use Swift to invoke OBJECTIVE-C code

Applications written by the Swift language have only just been submitted using the Xcode 6 GM version, and Objective-c has been in existence for many years as Apple's main development language. There is currently no mature swift library available, so the current writing application can be said to be basic without invoking the OBJECTIVE-C code of the case.

How to call Objective-c code

The solution now offered by Apple is to use a Bridging-header header file that references the header file of the OBJECTIVE-C code to be used. where Xcode automatically generates header filenames in the form of the project name-bridging-header.h. However, this file name format is not required and you are free to define it.

Xcode Automatic generation Scenario

Creating a new Swift project does not automatically generate a header file that calls Objective-c. However, you can create a new objective-c file in your project, and Xcode will prompt you to generate a corresponding header file to make Swift call. Choose Yes, this is the automatic generation of the above-mentioned format of the header file. You can delete the newly created empty objective-c file, leaving only the header file.
Fill in the header file of the Objective-c file or library that you want to call later in this bridge header file.

Custom Scenarios

If you choose to cancel in the previous step, the subsequent new Objective-c file will no longer be prompted. Or you want to customize a file name without using the filename that Xcode generates.

Select Create a new header file, the file name is arbitrary. For example, it could be Hello.h
After that, in the project-targets here, find the Swift compiler-code Generation , here is an item whose key is objective-c bridging Header , where its value is, fill in your header file information.

1. You can drag the corresponding header file into the input box of the fill value 2. Manually fill in the header file path into the input box, such as: hello/hello.h

This way, you can use the corresponding header file to refer to the OBJECT-C code.

Swift call objective-c code for iOS development

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.