[英]Raw type 'Bool' is not expressible by any literal
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
: 我想让我的枚举容易与
@IBInspectable
兼容,所以为了简单起见,我尝试用Bool
类型表示它:
enum TopBarStyle: Bool {
case darkOnLight
case lightOnDark
}
But Xcode is giving me: 但是Xcode给了我:
Raw type 'Bool' is not expressible by any literal
原始类型'Bool'不能通过任何文字表达
That's strange, as true
and false
seem to be perfect candidates for expressibility by literals. 这很奇怪,因为
true
和false
似乎是文字表达的完美候选人。
I've also tried to add RawRepresentable
conformance to type Bool with: 我还尝试将
RawRepresentable
一致性添加到Bool类型中:
extension Bool: RawRepresentable {
public init?(rawValue: Bool) {
self = rawValue
}
public var rawValue: Bool {
get { return self }
}
}
But it didn't solve the error. 但它没有解决错误。
Swift 3 natively defines those nine literal representations: Swift 3本身定义了九个文字表示:
nil
) nil
) false
) false
) []
) []
) [:]
) [:]
) 0
) 0
) 0.0
) 0.0
) "\\u{0}\u0026quot;
) "\\u{0}\u0026quot;
) "\\u{0}\u0026quot;
) "\\u{0}\u0026quot;
) ""
) ""
) But enum
raw representation will apparently only accept natively the subset of those representions that start with a digit ( 0
- 9
), a sign ( -
, +
) or a quote ( "
): the last five protocols of above list. 但
enum
原始表示显然只接受以数字( 0
- 9
),符号( -
, +
)或引号( "
)开头的那些代表的子集:上面列表的最后五个协议。
In my opinion, the error message should have been more specific. 在我看来,错误信息应该更具体。 Maybe something explicit like that would have been nice:
也许像这样明确的东西会很好:
Raw type 'Bool' is not expressible by any numeric or quoted-string literal
原始类型'Bool'不能由任何数字或带引号的字符串表示
Extending Bool to conform to one of those protocols is still possible, for example: 仍然可以扩展Bool以符合其中一个协议,例如:
extension Bool: ExpressibleByIntegerLiteral {
public init(integerLiteral value: Int) {
self = value != 0
}
}
And after doing so, this code now builds fine: 在这样做之后,这段代码现在构建良好:
enum TopBarStyle: Bool {
case darkOnLight
case lightOnDark
}
@IBInspectable var style = TopBarStyle(rawValue: false)!
my solution on swift 3 我在swift上的解决方案3
enum DataType: RawRepresentable {
case given
case received
typealias RawValue = Bool
var rawValue: RawValue {
return self == .given ? true : false
}
init?(rawValue: RawValue) {
self = rawValue == true ? .given : .received
}
}
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. 此外,如果可以实现的话,
TopBarStyle(rawValue: true)
将意味着什么并不清楚。
I would either use var darkOnLight: Bool
, or enum TopBarStyle { /*cases*/ }
and switch over the cases as necessary. 我要么使用
var darkOnLight: Bool
,要么enum TopBarStyle { /*cases*/ }
并根据需要切换案例。
Simplify your life: 简化你的生活:
enum TopBarStyle {
case darkOnLight
case lightOnDark
var bool: Bool {
switch self {
case .darkOnLight:
return true
default:
return false
}
}
}
Use as usual: 像往常一样使用:
var current = TopBarStyle.darkOnLight
if current.bool {
// do this
} else {
// do that
}
You can extend cases to more but they are not reversible since its an N : 2 matrix 您可以将案例扩展到更多但是它们不可逆,因为它是N:2矩阵
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.