iOS   发布时间:2022-03-30  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了ios – 优化级别会影响比较枚举大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
@H_801_0@
@H_801_0@
出于某些原因,优化级别快速我的比较方法返回额外的3个元素.这是我的代码的问题,还是 swift 2.0中的一个错误? XCode 7.0和XCode 7.1(2个不同的MaC)出现问题.

func ==(lhs: ViewController.ItemType,rhs: ViewController.ItemTypE) -> Bool {
    // For some reasons for different types e.g. .CType and .AType it returns true
    switch(lhs,rhs) {
    case (.AType,.ATypE):
        return true
    case (let .bType(type1),let .bType(type2)):
        return type1 == type2
    case (.CType,.CTypE):
        return true
    case (.DType,.DTypE):
        return true
    case (.EType,.ETypE):
        return true
    default:
        return false
    }
}

class ViewController: UIViewController {
    enum ItemType {
        case AType
        case BType(int)
        case CType
        case DType
        case EType
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        let array:[ItemType] = [.AType,.bType(10),.CType,.DType,.EType]
        let array2 = array.filter { (itemType:ItemTypE) -> Bool in
            return itemType == .CType
        }

        // Prints 1 on [-ONone] optimization and 4 for [-OFast] optimization.
        print("Items \(array2.count):\n\(array2)")
    }
}

解决方法

我一直在玩这个.这是一个最小的例子:

struct Foo {
    enum Enum {
        case A
        case B
        case C(int)
    }
}

func ==(lhs: Foo.Enum,rhs: Foo.Enum) -> Bool {
    print("comparing \(lhs) == \(rhs) -> ",terminator: "")

    switch(lhs,rhs) {
    case (.A,.A):
        return true
    case (.b,.b):
        return true
    default:
        return false
    }
}

func test() {
    print(  Foo.Enum.A          == .b)
    print([ Foo.Enum.A ][0]     == .b)
    print([ Foo.Enum.A ].first! == .b)
    for itemType in [ Foo.Enum.A ] { print(itemType == .b) }
}

test()

在-Onone构建此打印预期的四倍真.在优化的构建中,它打印…

comparing A == B -> false
comparing A == B -> false
comparing A == B -> true
comparing A == B -> true

错误在以下情况下消失:

>测试在外部文件范围内执行(不在函数中)
>使用普通函数代替operator ==
>枚举不是嵌套在另一种类型中
>删除案例C或关联类型
>打印语句插入到开关案例中

我一直在测试Xcode 7.1.这个bug肯定应该在bugreport.apple.com上提交

@H_801_0@

大佬总结

以上是大佬教程为你收集整理的ios – 优化级别会影响比较枚举全部内容,希望文章能够帮你解决ios – 优化级别会影响比较枚举所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签:ios优化影响枚举比较级别
猜你在找的iOS相关文章
其他相关热搜词更多
phpJavaPython程序员load如何string使用参数jquery开发安装listlinuxiosandroid工具javascriptcap