ios – 优化级别会影响比较枚举

ios – 优化级别会影响比较枚举,第1张

概述出于某些原因,优化级别快速我的比较方法返回额外的3个元素.这是我的代码的问题,还是 swift 2.0中的一个错误? XCode 7.0和XCode 7.1(2个不同的Mac)出现问题. func ==(lhs: ViewController.ItemType, rhs: ViewController.ItemType) -> Bool { // For some reasons for 出于某些原因,优化级别快速我的比较方法返回额外的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 -> falsecomparing A == B -> falsecomparing A == B -> truecomparing A == B -> true

该错误在以下情况下消失:

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

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

总结

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

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

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1003763.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-22
下一篇 2022-05-22

发表评论

登录后才能评论

评论列表(0条)

保存