Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 811
Cœur Avatar asked Feb 17 '17 07:02

Cœur


People also ask

What is a raw string literal in C++?

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 )”.

Why is Type X is not assignable to type Boolean?

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.

What is the difference between a literal and variable in typescript?

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.

What is raw string in C++ 11?

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 )”.


1 Answers

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

like image 63
Giuseppe Mazzilli Avatar answered Sep 17 '22 19:09

Giuseppe Mazzilli