Swift Framework 自定义 Module

关于在非framework项目中自定义 Module 可以看这篇文章, Swift 关于 module.modulemap 使用

关于 Framework Module

每个 framework 只可以有一个 module,如果不在Module Map File 这里配置自定义的 module,会自动生成一个,自动生成的 module里面,不包含在 framework 中自定义 module.modulemap 的内容。这样就会出现, 在其他项目导入后,报 Missing required module 这个错误。

Framework 自定义 实现

  1. 首先自定义一个 xxxx.modulemap, framework module ModuleFramework 这个 Module 的名字也必须是 YourProductName,因为系统自动生成就会是这样的格式,自定义的 framework module 只是为了替换系统自动生成的。
    image.png

    在自定义的 framework module 中你可以灵活的配置若干个子 module, 来处理各种文件。
framework module ModuleFramework {
    umbrella header "ModuleFramework.h"
    export *
    module * { export * }
    
    module OtherFile {
        module Subs {
            header "Sub.h"
            header "TestSubSub.h"
            header "SubSubs.h"
            header "Sub.h"
            header "Sub1.h"
            export *
        }
        
        module CFile {
            umbrella header "headers.h"
            export *
        }
    }

// 导出会附加在这里
module ModuleFramework.Swift {
    header "ModuleFramework-Swift.h"
    requires objc
}

如果自定义了module ,那么会自动生成 ModuleFramework.Swift, 添加在你自定义的 module 文件里面。

  1. Module Map File 中,配置你的 xxxx.modulemap 的相对路径,如下图

所有在headers 中体现的文件都需要加到 module 中,否则会有编译警告,这里可以愉快的配置权限。


  1. 关于 framework module 中使用 umbrella 递归导出文件夹内容,应该是不可以使用的。

  2. 关于 privateHeaders, 请查看 https://clang.llvm.org/docs/Modules.html#private-module-map-files

测试源码

reference: Link Static C Library to Swift Framework As A Private Module
reference: [https://developer.apple.com/library/archive/documentation/MacOSX/Conceptual/BPFrameworks/Concepts/CreationGuidelines.html#//apple_ref/doc/uid/20002254-BAJHGGGA](Import Code Within a Framework Target)
https://developer.apple.com/documentation/swift/imported_c_and_objective-c_apis/importing_objective-c_into_swift

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。