原始类型“布尔”不能用任何文字表示 [英] Raw type 'Bool' is not expressible by any literal
问题描述
我想让我的枚举轻松与 @IBInspectable
兼容,因此为了简单起见,我尝试将其枚举表示为 Bool类型。
:
I want to have my enums easily compatible with @IBInspectable
, so for the sake of simplicity, I tried to have it representable with type Bool
:
enum TopBarStyle: Bool {
case darkOnLight
case lightOnDark
}
但是Xcode给了我:
But Xcode is giving me:
原始类型'Bool'不能用任何文字表示
Raw type 'Bool' is not expressible by any literal
这很奇怪,因为 true
和 false
似乎是按文字表达的理想选择。
That's strange, as true
and false
seem to be perfect candidates for expressibility by literals.
我还尝试添加 RawRepresentable
一致性以使用以下类型输入Bool:
I've also tried to add RawRepresentable
conformance to type Bool with:
extension Bool: RawRepresentable {
public init?(rawValue: Bool) {
self = rawValue
}
public var rawValue: Bool {
get { return self }
}
}
但它没有解决错误
推荐答案
我认为这不是必需的。您可以制作一个普通的枚举,然后切换其大小写。同样,还不清楚 TopBarStyle(rawValue:true)
是什么意思。
I don't think this is necessary. You can just make a normal enum and then switch over its cases. Also, it's not clear at all what TopBarStyle(rawValue: true)
would mean if this could be achieved.
我可以使用 var darkOnLight:Bool
或 enum TopBarStyle {/ * cases * /}
并切换大小写
I would either use var darkOnLight: Bool
, or enum TopBarStyle { /*cases*/ }
and switch over the cases as necessary.
这篇关于原始类型“布尔”不能用任何文字表示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!