Create the main project and components

  1. Create the main project and addCocoaPods

  1. Create component modules,File - New - Project - FrameworkNext, fill in the component name and selectSwiftLanguage, next step,Add to, GroupSelect the main project and complete.

  1. Create a new class in the moduleMyBase.swiftUsed for testing

  1. Command + BCompile 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
  1. To expose the interface file in the module, go toBuild Phases - Headers - Public(The Swift project file is not added).

  1. Go to the main item,Targets -Build Phases - Link Binary With LibrariesTo addBaseModule.framework

  1. The main project,Targets - Build Setting - Search Paths - User Header Search pathsadd${SRCROOT}

CocoaPodsAdd 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