Create the main project and components
- Create the main project and add
CocoaPods
- Create component modules,
File - New - Project - Framework
Next, fill in the component name and selectSwift
Language, next step,Add to, Group
Select the main project and complete.
- Create a new class in the module
MyBase.swift
Used for testing
Command + B
Compile the component project, buildBaseModule.framework
Two, the use of modules
Add to the main project manually or via CocoaPods, choose one
Manually add modules to the main project
- To expose the interface file in the module, go to
Build Phases - Headers - Public
(The Swift project file is not added).
- Go to the main item,
Targets -Build Phases - Link Binary With Libraries
To addBaseModule.framework
- The main project,
Targets - Build Setting - Search Paths - User Header Search paths
add${SRCROOT}
CocoaPods
Add to the main project
platform :ios, '10.0'
use_frameworks!
workspace 'TestComponentDemo.xcworkspace'BaseModule component target'BaseModule' do# Configure the component path project'BaseModule/BaseModule.xcodeproj'End # Main project target'TestComponentDemo' do
end
Copy the code
Three, test use
//
// ViewController.swift
// TestComponentDemo
//
// Created by yuanzhiying on 2021/7/26.
//
import UIKit
import BaseModule
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
test()
testExtend()
}
func test() {
let base = MyBase()
base.openName = "modify open name"
base.publicName = "modify public name"
print(base.openName ?? "")
print(base.publicName ?? "")
base.openHelloWorld()
base.publicHelloWorld()
base.objcPublicHelloWorld()
}
func testExtend() {
let myExtend = MyClass()
print(myExtend.openIntro)
print(myExtend.publicIntro)
myExtend.openHelloWorld()
}
}
class MyClass: MyBase {
// The stored property cannot be override
// override var openName: String?
// override var publicName: String?
override var openIntro: String {
return "openIntro"
}
// Overriding non-open property outside of its defining module
// override var publicIntro: String? {
// return "publicIntro"
/ /}
override func openHelloWorld(){}// Overriding non-open instance method outside of its defining module
// override func publicHelloWorld() {}
// Overriding non-open instance method outside of its defining module
// override func objcPublicHelloWorld() {}
}
Copy the code