如何在XcodeGen项目中正确集成R.swift?
XcodeGen 集成 R.swift 解决方案
一、SPM 集成与 Build Tool Plug-in 配置
- 在
project.yml的packages节点添加 R.swift 依赖:
packages: Rswift: url: https://github.com/mac-cain13/R.swift.git from: 7.0.0
- 在目标 Target 的
dependencies中引入该包:
targets: YourTargetName: dependencies: - package: Rswift # ... 其他 Target 配置
- 在 Target 的
buildPhases中声明插件引用,XcodeGen 会自动将其添加到对应 Build Phases:
targets: YourTargetName: # ... 其他配置 buildPhases: - plugin: RswiftGeneratesInternalResources package: Rswift
生成项目后无需手动操作,插件会自动在构建时执行资源生成。
二、CocoaPods 集成解决「No such module 'Rswift'」问题
- 在
Podfile中添加 R.swift 依赖:
pod 'R.swift', '~> 7.0'
执行 pod install 完成安装。
- 在
project.yml的目标 Target 中添加自定义构建脚本,用于生成R.generated.swift:
targets: YourTargetName: # ... 其他配置 buildPhases: - name: Generate R.swift script: | "$PODS_ROOT/R.swift/rswift" generate "$SRCROOT/YourTargetName/R.generated.swift" --module YourTargetName inputFiles: - "$SRCROOT/YourTargetName/Assets.xcassets" - "$SRCROOT/YourTargetName/Base.lproj" outputFiles: - "$SRCROOT/YourTargetName/R.generated.swift"
注意:替换 YourTargetName 为实际的 Target 名称,根据项目资源路径调整 inputFiles。
- 将生成的
R.generated.swift加入 Target 的源码列表:
targets: YourTargetName: sources: - path: YourTargetName includes: - R.generated.swift # ... 其他配置
- 最后清理 Xcode 派生数据(
Command + Shift + K),关闭 Build Cache(File → Project Settings → Build Location → Advanced → Custom,取消勾选 Build Cache),重新构建项目即可解决模块找不到的问题。
内容的提问来源于stack exchange,提问作者Sergey
相关产品推荐
相关产品推荐

