【发布时间】:2021-11-09 06:09:39
【问题描述】:
我有下一个场景:
我创建了一个Swift package,我在main application 中使用它。在这个Swift Package 中,我想使用一些颜色,因为它是UI package。我的struct Colors 已经是defined in my main application,我不想在package 中再次定义它,所以我想将我的struct Colors 发送到package。
我的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'
有什么办法吗?
我想要的只是使用结构的值,我不需要更新它。
【问题讨论】:
-
为什么不简单地使用
Colors.General.mainColor为什么还要首先创建一个Colors 实例呢?错误很明显,你不能从实例访问静态属性,所以避免它,直接通过Colors.General.mainColor访问它我不太确定你的用例,但看看结构嵌套的方式和静态让似乎你可以采取更好的方法:| -
我已经编辑了帖子,说明了我如何发送它以及如何调用它,因为我希望在我的
package中使用该结构以及从main application发送的值跨度> -
如果需要作为值发送,需要一直是值,即Colors必须有General类型的属性。也就是说,感觉应该有更好的解决方案,更符合第一条评论。
标签: swift static swift-structs