Painted painted levels: fostered fostered fostered
Tags: “iOS” “Swift” “Swift and OC mix” author: Dac_1033 Review: QiShare team
In the process of iOS development using Swift, the mixed programming of Swift and OC is often involved. Sometimes the main project is OC, which needs to be coded into Swift code, and sometimes the main project is Swift, which needs to be coded into OC code. This involves modifying XCode’s engineering configuration, as well as a few other details, which we’ll cover below.
1. Mix in the same project
In Swift 5.1, the implementation steps of two kinds of mixed programming are as follows: Swift access OC: You only need to import OC classes to be exposed to Swift in the bridge file (productname-bridge-header. h) to access the corresponding OC classes and methods in Swift. OC accessing Swift: Import the productname-swift. h file into the OC class to access the attributes and methods exposed to the OC in Swift.
1.1 Swift Accesses the OC
For this article, we use an OC project as the host. To access OC from Swift, first create an empty project:
Create a Swift class in your project:
Create a bridge file with XCode guidance:
Import OC class header file in bridge file:
We can then use the OC class directly in the host project’s Swift:
import UIKit
class SwiftClass01: NSObject {
var con:ViewController?
override init() {
super.init()
self.con = ViewController()
}
}
Copy the code
In the host project, the OC class is used to access Swift. If you search for Swift Compiler in the Build Settings of the project configuration, you can see two configuration files mixed with Swift and OC:
Import productname-swift. h into the OC class that wants to access the Swift method (manually entered without prompting and red before compilation) and compile:
Swift can be called in the OC class ViewController:
In this example, click on the header file qihybridcompile-swift. h to see its definition in the project:
- In Swift class, all methods that allow OC access must be preceded by @objc;
- The Swift class uses a public modified method to appear in the productname-swift.h file.
- All Swift classes are automatically registered in the productName-swift.h file for automatic @interface modifications, and the productname-swift.h file is automatically updated.
2. Mixing between the Framework and the host APP
First, create a Swift project that wraps the framework, called “Qiswiftsdk.xcodeProj”, and drag it into the host project. Then, under BuildPhase in the host project configuration, set the project dependencies as shown below:
In this example, create a Swift class “QSSdkTest” in OC host project, and write two test methods, then call Swift library code as follows:
- Swift library to expose the class, before the class need to use the keyword open modification;
- @objc public: @objc public: @objC public: @objC public
- In this example, the default header file “qiswiftsdk.h” does nothing;
- Swift library can also be mixed, Swift library engineering configuration also bridge file related configuration.
Recommended articles:
IOS UI state save and restore (three) iOS UI state save and restore (two) iOS UI state save and restore (one) iOS accurate timing of the common method Sign In With Apple (one) algorithm small column: Dynamic programming (I) Dart Foundation (I) Dart Foundation (II) Dart Foundation (III) Dart Foundation (IV) Odd Dance Weekly