1

我有下一个场景

我创建了一个Swift package我正在使用的main application. 在这Swift Package我想使用一些颜色,因为它是一个UI package. 我struct Colors的已经是defined in my main application,我不想在 中再次定义它,package所以我试图将我struct Colorspackage.

struct Colors还有另一个struct General像:

struct Colors {
     struct General {
          static let mainColor = "5F8E3F".toColor
     }
}

这就是我在我的package

func setupContent(withMainStruct mainStruct: Colors) {
    print(mainStruct.General.mainColor)
}

这就是我发送它的方式main application

let mainStruct = ColorStruct()
cell.setupContent(withMainStruct: mainStruct)

我的主要问题是:

Static member 'General' cannot be used on instance of type 'Colors'

有没有办法做到这一点?

我想要的只是使用结构的值,我不需要更新它。

4

1 回答 1

2

听起来您想在您的包中定义一个协议,该协议由您的应用程序中的结构实现。因此,您只需将这些预定义值提供给包即可。

所以像这样。

在应用程序中:

struct AppColors: Colors {
    let general: GeneralColors = General()

    struct General: GeneralColors {
        let mainColor = UIColor.blue
     }
}

configLib(colors: AppColors())

包装内

protocol Colors {
    var general: GeneralColors { get }
}

protocol GeneralColors {
    var mainColor: UIColor { get }
}

func configLib(colors: Colors) {
    print(colors.general.mainColor)
}
于 2021-11-09T10:35:03.367 回答