IOS development-call Objective-C code through Swift

Source: Internet
Author: User

IOS development-call Objective-C code through Swift

Swift, a new programming language developed in iOS recently, has a large response in the iOS development circle. There are many tutorials or small examples at home and abroad. Although the Swift language is still evolving, and the changes are still relatively large, Apple does not promise that the Code currently written will be compatible in the future, but it still cannot block the enthusiasm of iOS developers.

Why use Swift to call Objective-C code?

Currently, applications written in the Swift language can only be submitted using Xcode 6 GM, while Objective-C has been Apple's main development language for many years. Currently, no mature Swift library is available, so currently writing applications is basically inseparable from calling Objective-C code.

How to call Objective-C code

Currently, the solution provided by Apple is to use a Bridging-Header file to reference the Header file of the Objective-C code to be used. Xcode automatically generates header file names in the form of project name-Bridging-Header.h. However, the file name format is not mandatory and can be defined freely.

Xcode Automatic Generation Scheme

Creating a new Swift project does not automatically generate a header file that calls Objective-C. However, you can create an Objective-C file in the project. At this time, Xcode will prompt you whether to generate a corresponding header file for Swift to call. SelectYesIn this case, a header file in the preceding format is automatically generated. You can delete the newly created empty Objective-C file and only keep this header file.
Fill in the header file of the Objective-C file or library you want to call later in this bridge header file.

Custom Solution

If you cancel the creation in the previous step, you will not be prompted to create the Objective-C file later. Or you want to customize a file name without using the file name generated by Xcode.

Select to create a new Header file. The file name is random. For example, it can be Hello. h.
ThenProject-TARGETSHere, findSwift Compiler-Code GenerationFor this item, the key isObjective-C Bridging HeaderEnter your header file information in the Value Field.

 
 
  1. 1. You can drag the corresponding header file to the input box of the filled value.
  2. 2. manually enter the header file path to the input box, such as Hello/Hello. h.

In this way, you can use the corresponding header file to reference the Object-C code.

From: http://my.oschina.net/dmdgeeker/blog/318356

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.