[英]Color Extension with asset catalog colors in SwiftUI crashing preview
The below example compiled and ran fine in simulator/device, but runs into problems in canvas preview.以下示例在模拟器/设备中编译并运行良好,但在 canvas 预览版中遇到问题。 (I have tried clearing cache and derived data)
(我试过清除缓存和派生数据)
extension Color {
static let primary = Color("primary")
static let test = Color(red: 1, green: 0, blue: 0)
}
This works fine in canvas preview:这在 canvas 预览版中工作正常:
Text("hi").background(Color.test)
But color assets crashes preview:但是颜色资产崩溃预览:
Text("hi").background(Color.primary)
Compiling failed: cannot convert return expression of type 'AnyView' to return type 'some View'
(Also as a side note, this seems to work just fine Text("hi").background(Color("primary"))
) (另外作为旁注,这似乎工作得很好
Text("hi").background(Color("primary"))
)
There is SwiftUI standard Color.primary
(see below), so I assume the issue is due to ambiguity.有 SwiftUI 标准
Color.primary
(见下文),所以我认为问题是由于模棱两可。
@available(iOS 13.0, OSX 10.15, tvOS 13.0, watchOS 6.0, *) extension Color { /// A set of colors that are used by system elements and applications. public static let clear: Color... public static let primary: Color // << here !!
use instead unique static var name for your custom color, like使用独特的 static var name 代替您的自定义颜色,例如
extension Color {
static let myPrimary = Color("primary") // << here !!
static let test = Color(red: 1, green: 0, blue: 0)
}
Tested with Xcode 11.4用 Xcode 11.4 测试
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.