【问题标题】:float is not convertible to 'MirrorDisposition' Swift What is mirrordisposition?float 不能转换为 'MirrorDisposition' Swift 什么是 mirrordisposition?
【发布时间】:2014-07-22 15:42:58
【问题描述】:

我得到以下代码:

let floatValue: Float = 1
let intValue: Int = 1

if floatValue == intValue   {
    println("Types and value are equal")
} else {
    println("Type is not equal.")
}

我知道它会打印“类型不相等”。

但我在if floatValue == intValue 遇到错误 错误是:

Float 不能转换为 'MirrorDisposition'

我以前从未见过这个错误,也找不到任何关于它的信息。 此代码在 xCode 6 beta 1、2 和 3 中运行良好。 我现在正在运行 xCode 6 beta 4。

有谁知道这个错误是什么意思?以及我最终能做些什么。

【问题讨论】:

    标签: swift


    【解决方案1】:

    MirrorDisposition 是您可以从值的Mirror 获得的类型之一(使用reflect 函数)。它们是为 IDE 制作的,用于显示值。

    /// How children of this value should be presented in the IDE.
    enum MirrorDisposition {
        case Struct
        case Class
        case Enum
        case Tuple
        case Aggregate
        case IndexContainer
        case KeyContainer
        case MembershipContainer
        case Container
        case Optional
        case ObjCObject
    }
    

    错误消息意味着编译器没有找到== 运算符来比较FloatInt。但是,它可能为MirrorDispositionInt 找到了== 运算符,因此它正在尝试将Float 转换为MirrorDisposition,但显然不能,因此您会收到一条错误消息。

    (顺便说一句,你得到的类型错误是随机的,取决于编译器尝试使用的运算符。我得到的是Float is not convertible to Selector)。

    错误信息是一个bug,应该有一条信息说Could not find == operator for Float and Int

    检查值相等的明显修复方法是使用强制转换:

    if intValue == Int(floatValue)  {
    

    没有理由以这种方式比较类型,因为在 Swift 中,类型是由编译器检查的。永远不应该有明确检查类型的理由(当然是关于值类型,而不是对象类型)。

    【讨论】:

    • 感谢您的扩展回答,非常有用。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-04-26
    • 2014-11-13
    • 1970-01-01
    • 2023-02-14
    • 2010-12-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多