After the Swift language comes out, new projects may be developed directly using Swift, but in the process, you may encounter some situations where you don’t want to rewrite some classes or encapsulated modules written in OC in Swift, so you can use mixed compilation. This is allowed in iOS 8. Let's start with the simple one and study the mixed use in the same project directory. For demonstration. First prepare two classes The first one is a class written in Swift, the file name is act.swift
The second one is written in OC. The class header file is OCChannel.h and the implementation file is OCChannel.m Header Files
Implementation Files
This OCChannel references the Act class written in Swift. It is mainly to demonstrate that in the same project, the Swift class calls OC, and the OC class also calls Swift, thus forming a mixed writing mode. Here are the specific steps: 1. Create a new Swift project: My project is called MixDemo After the project is completed: 2. Import the two previous classes separately. Let's do it one by one. Because we are building Swift, let's first mix and compile by referencing the OC file in the Swift project. Using OC in Swift First of all, Swift no longer uses header files and .m files. So there is no need to use import "" to import header files. How can Swift access OC class declarations? In fact, Swift also needs to use header files for access, but it no longer needs to be imported explicitly. There are two ways to generate this header file. Method 1: In a brand new Swift, use the first new creation prompt to automatically add a bridging header file. After clicking OK, a header file named <produceName-Bridging-Header.h> will be generated. The completed project: One thing you need to pay attention to here is to set the bridging header file in targets->build settings->Object-C Bridging Header. After the above steps, the bridging file is ready. Feel free to put the Objective-C header files you want to call in your Swift class into this bridge file using import "". Like:
Similarly, once you know the relationship between Swift search header files, you don't need to care about the -Bridging-Header.h file anymore. You can create one manually and give it a name you like. For example: Method 2: Create a new header file named: OCContainerHeader.h OK, the above settings fully satisfy the classes written in Swift using OC.
Now let's take a look at how OC calls a class written in Swift. (In fact, if you copied my DEMO exactly, then congratulations, you will not be able to compile the above. Because my OC class references a class written in Swift, so if you want to run it, you must comment the class of that Act.) How to call a class written in Swift in OC To use OC, you must have a header file. But Swift files do not have header files, so we must also generate a header file. But the header file for OC to call Swift is quite special. Because the mechanism in the header file is automatically generated, it is not recommended to write it by hand if you are not familiar with it. How to generate this header file. (Note that the header file set by the system cannot be seen in the project.) Generation steps: Select targets->build settings->packing->Product Module Name and set the module name. This name is very important. The Swift header file is named according to this. In this way, if you want to use OC in Swift in your project, you can write all the header files of the OC classes you need to use in MixDemo-Bridging-Header.h. Similarly, if you use the Swift class in OC, you only need to clean it and then compile it, but don't forget to import SwiftModule-swift.h (the name is up to you, but -swift.h is fixed). There is another thing that readers need to pay attention to. Note: If a class written in Swift does not inherit from NSObject or a derived class of NSObject, the corresponding conversion class will not be generated after compilation, so that the corresponding declaration cannot be found in OC. For example, class Act in my example will not be compiled into SwiftModule-swift.h, but if it is written as class Act : NSObject, the corresponding declaration can be compiled. You can also use @objc to declare it, but this is still the same, class *** inherits NSObject. Like below:
But when you use it, you will find act = [[Act alloc]init]; //Error, alloc not found, so it is recommended that you inherit NSObject. |
<<: Cyanogen and Microsoft reach strategic partnership to bundle Microsoft apps
>>: Swift TIP: objc and dynamic
When the prices of 65-inch TVs from some well-kno...
Each time the official account calls an interface...
With the strategic upgrades of major platforms an...
When checking keyword rankings during website opt...
The sense of smell is one of the earliest senses ...
Starting with "Surround the Nervous Cat"...
When it comes to the work content of operations, ...
The popularity of Tik Tok has redefined the mobil...
Sun Jing's 14-day course resources introducti...
Huang Daozhu's analysis of Taobao's virtu...
Author: Gong Weiyan, Associate Researcher, Instit...
WeChat is still popular, and its 800 million user...
Topic: "Unlocking a new world: the latest Ba...
Copywriting is not about creating words and sente...
If you have free time, you must watch TV series! ...