Search code examples
swiftenumsrawrepresentable

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:

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.


Solution

  • 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.

    I would either use var darkOnLight: Bool, or enum TopBarStyle { /*cases*/ } and switch over the cases as necessary.