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
}
But Xcode is giving me:
Raw type 'Bool' is not expressible by any literal
That's strange, as true
and false
seem to be perfect candidates for expressibility by literals.
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 }
}
}
But it didn't solve the error.
A raw string literal is a string in which the escape characters like , or \” of C++ are not processed. Hence, this was introduced in C++11, a raw string literal which starts with R” ( and ends in )”.
The cause of the "Type 'X' is not assignable to type boolean" error is that the types of the values on the left an right hand sides are not compatible. So depending on your use case, you could solve the error by updating the type of the value to the left or right and making them compatible.
This is in contrast to the variable which allows you to change value (except for TypeScript Constants). The latest version of Typescript supports the String Literal Types, Numeric Literal Types, Boolean Literal Types & Enum Literal Types A literal is a notation for representing a fixed value in the source code.
From C++ 11, we can use raw strings in which escape characters (like or \” ) are not processed. The syntax of raw string is that the literal starts with R” ( and ends in )”.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With