Recently, Swift, a new programming language for iOS development, has received a lot of feedback in the iOS development circle, and there are many tutorials or small examples at home and abroad.
Although the language Swift is still evolving and has changed a lot, Apple does not promise that the code it currently writes will be compatible in the future, it still cannot stop the enthusiasm of iOS developers.
Why use Swift to call Objective-C code
Currently, applications written in Swift can be submitted using Xcode 6 GM version, and Objective-C has existed as Apple's main development language for many years. There is currently no mature Swift library available, so writing applications can be said to be basically inseparable from calling Objective-C code.
How to call Objective-C code
The solution given by Apple is to use a Bridge-Header header file to reference the header file of the Objective-C code you want to use. The header file name automatically generated by Xcode will be in the form of project name - this way. However, this file name format is not necessary and you can freely define it.
Xcode automatic generation scheme
Creating a new Swift project will not automatically generate the header file that calls Objective-C. However, you can create a new Objective-C file in the project. At this time, Xcode will prompt you whether to generate a corresponding header file to make Swift call. Select Yes, and at this time, a header file in the format mentioned above will be automatically generated. You can delete the newly created empty Objective-C file and only retain this header file.
Just fill in the header file of the Objective-C file or library you want to call in the subsequent header file.
Customize the solution
If you chose Cancel in the previous step, there will be no prompts for creating a new Objective-C file in the future. Or you want to customize a filename without using Xcode generated filename.
Select Create a new Header file with the file name as you like. For example, it can be
After that, in the project - TARGETS, find the Swift Compiler - Code Generation item. There is an item here. The key is Objective-C Bridge Header. Just fill in your header file information in the value.
1. You can drag the corresponding header file into the input box where the value is filled.
2. Manually fill in the header file path into the input box, such as: Hello/
This way, you can use the corresponding header file to reference the Object-C code.