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

SwiftUI跨Swift Package访问资源报错:找不到Theme_Theme bundle

问题:Swift Packages跨包访问资源报错unable to find bundle named Theme_Theme

我正在开发一个采用Swift Packages模块化的项目,Theme包的Sources目录下存放着Color和Image资源。在Home包中尝试访问Theme包的资源时,出现错误提示unable to find bundle named Theme_Theme,但主Target中可正常访问相关资源。


Theme.swift

public struct PrimaryTheme {
    public init() {}
    public static var smallTextSize: CGFloat = 14.0
    public static var mediumTextSize: CGFloat = 16.0
    public static var largeTextSize: CGFloat = 24.0
    public static var navigationTextSize: CGFloat = 18.0
    public static var smallPadding: CGFloat = 14.0
    public static var mediumPadding: CGFloat = 18.0
    public static var largePadding: CGFloat = 25.0
    public enum Images: String {
        case bill, tinggSplashScreenIcon, tinggAssistImage, tinggIcon,
        addBillImage, moneyImage, home, explore, group
        public var image: Image {
            return Images.getImage(self.rawValue)
        }
        public static func getImage(_ name: String) -> Image {
            return Image(name, bundle: Bundle.module)
        }
    }
    public enum AppColors: String {
        case secondaryColor, primaryColor, cellulantPurple,
             cellulantRed, skyBlue, cellulantLightGray, textColor
        public static func getColor(_ name: String) -> Color {
            return  Color(name, bundle: Bundle.module)
        }
    }
    public static func getColor(_ name: AppColors) -> Color {
        return  AppColors.getColor(name.rawValue)
    }
    public static func getImage(image: Images) -> Image {
        return Images.getImage(image.rawValue)
    }
}

Theme Package配置

import PackageDescription

let package = Package(
    name: "Theme",
    platforms: [.iOS(.v13)],
    products: [
        .library(
            name: "Theme",
            targets: ["Theme"])
    ],
    dependencies: [],
    targets: [
        .target(
            name: "Theme",
            dependencies: [],
            resources: [.copy("Color.xcassets")]
        ),
        .testTarget(
            name: "ThemeTests",
            dependencies: ["Theme"])
    ]
)

Home.swift

import SwiftUI
import Theme
struct HomeUIView: View {
    var body: some View {
        VStack {
            Text("Hello")
                .foregroundColor(PrimaryTheme.getColor(.primaryColor))
        }
    }
}

struct HomeUIView_Previews: PreviewProvider {
    static var previews: some View {
        HomeUIView()
    }
}

Home Package配置

import PackageDescription

let package = Package(
    name: "Home",
    platforms: [.iOS(.v13)],
    products: [
        .library(
            name: "Home",
            targets: ["Home"]),
    ],
    dependencies: [
        .package(path: "../Theme")
    ],
    targets: [
        .target(
            name: "Home",
            dependencies: ["Theme"]),
        .testTarget(
            name: "HomeTests",
            dependencies: ["Home", "Theme"])
    ]
)

问题原因与修复方案

问题原因

  1. 资源处理方式错误:Theme包中使用.copy("Color.xcassets")只是简单复制目录,没有让Swift Package正确编译Asset Catalog资源。
  2. 跨包bundle访问限制:Bundle.module是Swift Package自动生成的内部属性,跨包访问时,依赖包(Home)无法正确定位到Theme包的资源bundle,导致运行时找不到Theme_Theme bundle。

修复步骤

  1. 修正Theme包的资源处理逻辑
    在Theme Package的target配置中,将.copy("Color.xcassets")替换为.process("Color.xcassets"),让Swift Package正确编译Asset Catalog:

    .target(
        name: "Theme",
        dependencies: [],
        resources: [.process("Color.xcassets")] // 修改为process
    )
    
  2. 公开Theme包的bundle实例
    在PrimaryTheme结构体中添加公开的静态bundle属性,替代内部的Bundle.module,确保跨包可以正常访问:

    public struct PrimaryTheme {
        // 新增:公开Theme包的bundle
        public static let resourceBundle = Bundle.module
        
        // 原有代码保持不变...
        
        public enum Images: String {
            // ...
            public static func getImage(_ name: String) -> Image {
                // 改用公开的resourceBundle
                return Image(name, bundle: PrimaryTheme.resourceBundle)
            }
        }
        
        public enum AppColors: String {
            // ...
            public static func getColor(_ name: String) -> Color {
                // 改用公开的resourceBundle
                return Color(name, bundle: PrimaryTheme.resourceBundle)
            }
        }
    }
    
  3. 清理缓存并重新构建
    在Xcode中执行Cmd+Shift+K清理项目缓存,然后重新构建整个项目,确保资源bundle被正确嵌入到依赖包中。

  4. 预览页面的额外处理(可选)
    如果Home包的预览仍然报错,可以在预览代码中显式指定资源bundle:

    struct HomeUIView_Previews: PreviewProvider {
        static var previews: some View {
            HomeUIView()
                .onAppear { _ = PrimaryTheme.resourceBundle } // 提前加载bundle
        }
    }
    

验证修复

完成上述修改后,重新运行项目,Home包即可正常访问Theme包的Color和Image资源,不再出现找不到bundle的错误。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.25 11:06:19