简体   繁体   English

原始类型'Bool'不能通过任何文字表达

[英]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. 这很奇怪,因为truefalse似乎是文字表达的完美候选人。

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本身定义了九个文字表示:

  • ExpressibleByNilLiteral ( nil ) ExpressibleByNilLiteral( nil
  • ExpressibleByBooleanLiteral ( false ) ExpressibleByBooleanLiteral( false
  • ExpressibleByArrayLiteral ( [] ) ExpressibleByArrayLiteral( []
  • ExpressibleByDictionaryLiteral ( [:] ) ExpressibleByDictionaryLiteral( [:]
  • ExpressibleByIntegerLiteral ( 0 ) ExpressibleByIntegerLiteral( 0
  • ExpressibleByFloatLiteral ( 0.0 ) ExpressibleByFloatLiteral( 0.0
  • ExpressibleByUnicodeScalarLiteral ( "\\u{0}\u0026quot; ) ExpressibleByUnicodeScalarLiteral( "\\u{0}\u0026quot;
  • ExpressibleByExtendedGraphemeClusterLiteral ( "\\u{0}\u0026quot; ) ExpressibleByExtendedGraphemeClusterLiteral( "\\u{0}\u0026quot;
  • ExpressibleByStringLiteral ( "" ) ExpressibleByStringLiteral( ""

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.

相关问题 原始类型“错误”不能用任何文字表示 - Raw type 'Error' is not expressible by any literal Swift 枚举自定义原始类型不能由不同的文字表达 - Swift enum custom raw type not expressible by different literals Swift:无法转换类型“(Any) throws -> Bool”的值? 到预期的参数类型 '(Any) throws -> Bool? - Swift: Cannot convert value of type '(Any) throws -> Bool? to expected argument type '(Any) throws -> Bool? 如何将“Any”类型的原始数据转换为具体类型? - How do I convert raw data of type 'Any' into a concrete type? Swift:无法使用类型为(((Any)throws-> Bool)'的参数列表调用'filter' - Swift: Cannot invoke 'filter' with an argument list of type '((Any) throws -> Bool)' 上下文类型“Any”不能与数组文字Swift 3一起使用 - Contextual type 'Any' cannot be used with array literal Swift 3 Swift上下文类型any不能与数组文字一起使用 - Swift contextual type any cannot be used with array literal 上下文类型“Any”不能与字典文字Swift一起使用 - Contextual type 'Any' cannot be used with dictionary literal Swift 无法将'String'类型的值转换为预期的参数类型'(Any)引发-> Bool' - Cannot convert value of type 'String' to expected argument type '(Any) throws -> Bool' 枚举案例的原始值必须是文字 - Raw value for enum case must be a literal
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM