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

如何在XcodeGen项目中正确集成R.swift?

XcodeGen 集成 R.swift 解决方案

一、SPM 集成与 Build Tool Plug-in 配置

  1. 在 project.yml 的 packages 节点添加 R.swift 依赖:
packages:
  Rswift:
    url: https://github.com/mac-cain13/R.swift.git
    from: 7.0.0
  1. 在目标 Target 的 dependencies 中引入该包:
targets:
  YourTargetName:
    dependencies:
      - package: Rswift
    # ... 其他 Target 配置
  1. 在 Target 的 buildPhases 中声明插件引用,XcodeGen 会自动将其添加到对应 Build Phases:
targets:
  YourTargetName:
    # ... 其他配置
    buildPhases:
      - plugin: RswiftGeneratesInternalResources
        package: Rswift

生成项目后无需手动操作,插件会自动在构建时执行资源生成。

二、CocoaPods 集成解决「No such module 'Rswift'」问题

  1. 在 Podfile 中添加 R.swift 依赖:
pod 'R.swift', '~> 7.0'

执行 pod install 完成安装。

  1. 在 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。

  1. 将生成的 R.generated.swift 加入 Target 的源码列表:
targets:
  YourTargetName:
    sources:
      - path: YourTargetName
        includes:
          - R.generated.swift
    # ... 其他配置
  1. 最后清理 Xcode 派生数据(Command + Shift + K),关闭 Build Cache(File → Project Settings → Build Location → Advanced → Custom,取消勾选 Build Cache),重新构建项目即可解决模块找不到的问题。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.23 09:57:17