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

添加RxSwift依赖后Swift包构建失败:无法找到RxCocoa产品

解决Swift包添加RxSwift依赖的问题

问题分析

你遇到的product 'RxCocoa' required by package 'mylibrary' target 'MyLibrary' not found错误,主要源于两个问题:

  1. Package.swift的products段存在无效的注释残留文本,导致语法解析异常
  2. 引用RxCocoa依赖时未明确指定它所属的RxSwift包,SPM无法定位到该产品

修正后的Package.swift代码

// swift-tools-version: 5.8
import PackageDescription

let package = Package(
    name: "MyLibrary",
    products: [
        .library(
            name: "MyLibrary",
            targets: ["MyLibrary"]),
    ],
    dependencies: [
        .package(url: "https://github.com/ReactiveX/RxSwift.git", .exact("6.5.0"))
    ],
    targets: [
        .target(
            name: "MyLibrary",
            dependencies: [
                "RxSwift",
                .product(name: "RxCocoa", package: "RxSwift")
            ]),
        .testTarget(
            name: "MyLibraryTests",
            dependencies: ["MyLibrary"]),
    ]
)

关键修改点

  • 移除无效文本:删除products数组中多余的them visible to other packages.文本,修复语法错误
  • 明确指定RxCocoa所属包:将target依赖中的"RxCocoa"改为.product(name: "RxCocoa", package: "RxSwift"),告诉SPM该产品来自RxSwift依赖包

后续操作

  1. 保存修改后的Package.swift
  2. 在Xcode中执行File > Packages > Resolve Package Versions,重新解析依赖
  3. 清理项目(Cmd+Shift+K)后重新构建,错误即可解决

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.24 17:02:43