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
Recently, NASA has demonstrated whether it is pos...
Image source: pixabay We can still heed Rachel’s ...
Every time I hear the prelude of "Gong Xi Fa...
WeChat is no longer just an IM. Since WeChat 4.X,...
1. Case highlights 1. One gameplay brought at lea...
If you know a little about wine, you will find th...
Today I want to talk to you about big data and ac...
I have read an article by Li Jiaoshou before, &qu...
1. The key to the current counter-trend growth: u...
Oh my god, I was really blown away. It turns out ...
Until now, WeChat mobile software has become a so...
The author of this article will introduce some st...
Now we often hear that taking children to outdoor...
Friends who don’t pay much attention to the knowl...
As a classic game console, PS4 has accompanied gl...