You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

XCFramework编译后swiftinterface仅显单个类,如何暴露所有公开成员?

解决XCFramework中公开类/属性无法被外部访问的问题

我正在构建用于分发的XCFramework,已将需要暴露的所有类和属性标记为open或public。框架内包含多个类和文件,但生成的.swiftinterface文件仅引用单个类,导致导入该框架的项目无法访问未被引用的属性。

生成的.swiftinterface内容

// swift-interface-format-version: 1.0
// swift-compiler-version: Apple Swift version 6.0.2 effective-5.10 (swiftlang-6.0.2.1.2 clang-1600.0.26.4)
// swift-module-flags: -target arm64-apple-ios14.5-simulator -enable-objc-interop -enable-library-evolution -swift-version 5 -enforce-exclusivity=checked -O -enable-bare-slash-regex -module-name MyFramework
// swift-module-flags-ignorable: -no-verify-emitted-module-interface
import Foundation
@_exported import MyFramework
import Swift
import _Concurrency
import _StringProcessing
import _SwiftConcurrencyShims
open class MyFrameworkClass : Swift.Decodable {
  open var url: Swift.String?
  open var parameters: MyFrameworkProperty.Parameters?
  public init(url: Swift.String? = nil, parameters: MyFrameworkProperty.Parameters? = nil)
  @objc deinit
  required public init(from decoder: any Swift.Decoder) throws
}
open class Parameters : Swift.Decodable {
  open var PropertyA: Swift.String?
  open var PropertyB: Swift.String?
  open var PropertyC: Swift.String?
  public init(PropertyA: Swift.String? = nil, PropertyB: Swift.String? = nil, PropertyC: Swift.String? = nil)
  @objc deinit
  required public init(from decoder: any Swift.Decoder) throws
}
public enum PropertyC : Swift.String, Swift.CaseIterable {
  case a
  case b
  case c
  public init?(rawValue: Swift.String)
  public typealias AllCases = [MyFramework.PropertyC]
  public typealias RawValue = Swift.String
  nonisolated public static var allCases: [MyFramework.PropertyC] {
    get
  }
  public var rawValue: Swift.String {
    get
  }
}
extension MyFramework.PropertyC : Swift.Equatable {}
extension MyFramework.PropertyC : Swift.Hashable {}
extension MyFramework.PropertyC : Swift.RawRepresentable {}

使用的编译命令

xcodebuild archive -workspace MyFramework.xcworkspace \
-scheme MyFramework \
-configuration Release \
-destination 'generic/platform=iOS Simulator' \
-archivePath './build/MyFramework.framework-iphonesimulator.xcarchive' \
SKIP_INSTALL=NO \
BUILD_LIBRARIES_FOR_DISTRIBUTION=YES

xcodebuild archive -workspace MyFramework.xcworkspace \
-scheme MyFramework \
-configuration Release \
-destination 'generic/platform=iOS' \
-archivePath './build/MyFramework.framework-iphoneos.xcarchive' \
SKIP_INSTALL=NO \
BUILD_LIBRARIES_FOR_DISTRIBUTION=YES

xcodebuild -create-xcframework \
-framework './build/MyFramework.framework-iphonesimulator.xcarchive/Products/Library/Frameworks/MyFramework.framework' \
-framework './build/MyFramework.framework-iphoneos.xcarchive/Products/Library/Frameworks/MyFramework.framework' \
-output './build/MyFramework.xcframework'

编译前的MyFramework.h内容

#import <Foundation/Foundation.h>

FOUNDATION_EXPORT double MyFrameworkVersionNumber;

FOUNDATION_EXPORT const unsigned char MyFrameworkVersionString[];

解决操作步骤:

  • 显式导出所有公开类型:检查框架的主Swift入口文件(如MyFramework.swift),确保所有需要暴露的open/public类、枚举、结构体都被包含在模块接口中。如果类型分散在不同文件,要么将这些文件添加到目标的Compile Sources列表,要么在主文件中通过@_exported import导出子模块,或者添加空扩展强制编译器导出:
    // 强制导出未被引用的公开类型
    public extension OtherPublicClass {}
    
  • 检查模块映射配置:如果是混编框架,确认module.modulemap配置正确,保证所有符号被导出:
    framework module MyFramework {
        umbrella header "MyFramework.h"
        export *
        module * { export * }
    }
    
  • 验证Build Settings:确认BUILD_LIBRARIES_FOR_DISTRIBUTION为YES,Release配置下SWIFT_COMPILATION_MODE设为Whole Module,确保编译器能扫描到所有公开类型。
  • 清理缓存重新编译:执行xcodebuild clean清理缓存,再重新执行归档和XCFramework创建命令,避免缓存导致的接口不完整问题。
  • 检查访问级别与引用关系:确保未被访问的公开类型没有被编译器优化剔除,要么让它们被其他公开类型引用,要么通过上述空扩展的方式强制导出。

内容的提问来源于stack exchange,提问作者thesuffering

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.15 19:04:53