Swift 2.1错误排序到位,只有在发行版本上 [英] Swift 2.1 Error sorting in place, only on release builds

查看:115
本文介绍了Swift 2.1错误排序到位,只有在发行版本上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开始收到以下代码中的排序lamdba的崩溃报告,灰色框中的第三行:

I started getting crash reports for the sort lamdba in the below code, the third line in the grey box below:

private func fixOverlaps(inout blocks: [TimeBlock], maxOverlaps: Int? = nil) {
    blocks.sortInPlace { a,b in
        if a.startTime < b.startTime {
            return true
        } else if a.startTime == b.startTime && a.endTime < b.endTime {
            return true
        }
        return false
    }
...

请注意,从XCode调试生成时不会发生崩溃。只有App Store和Ad Hoc存档会崩溃,并且只有当块列表的长度在几百个。

Note the crash does not occur on debug builds from XCode. Only the App Store and Ad Hoc archives will crash, and only when the length of the blocks list is in the hundreds.

我修改代码到这里,问题去掉:

I modified the code to this, and the problem went away:

private func fixOverlaps(inout blocks: [TimeBlock], maxOverlaps: Int? = nil) {
    blocks = blocks.sort { a,b in
        if a.startTime < b.startTime {
            return true
        } else if a.startTime == b.startTime && a.endTime < b.endTime {
            return true
        }
        return false
    }
...

有没有什么我错过了如何使用inout或sortInPlace?我可以尝试做一个演示这个。它在多个版本的iOS(8/9)和Swift 2.1。

Is there something I've missed about how to use inout or sortInPlace? I can try to do a demo of this. It's on multiple versions of iOS (8/9) and Swift 2.1.

EDIT ------------------- -

EDIT--------------------

Ok这里是一个最小的版本崩溃。结果是inout是一个红色的鲱鱼。如果在XCode 7.1中启动一个新的单视图项目,你可以用下面的代替视图控制器:

Ok here's a minimal version that crashes. Turns out the inout was a red herring. If you start a new single view project in XCode 7.1, you can replace the view controller with this:

class ViewController: UIViewController {

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

    var blocks = [TimeBlock]()
    for var i in 0...20 { //Works if you put in a small number like 8
        let t = TimeBlock()
        t.start = Int(arc4random_uniform(1000)) //Get some random numbers so the sort has to do some work
        t.end = Int(arc4random_uniform(1000))
        blocks.append(t)
    }

    blocks.sortInPlace { a,b in
        if a.start > b.start {
            return true
        }
        return false
    }

    print("done") //Gets here on debug, not release
}

class TimeBlock {
    var start = 0
    var end = 0
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}


}

在发布中,如果你在17左右结束循环,你会看到它打印出完成,但是崩溃了20.精确的数字可能会有所不同。

So run it in release, and you should see it prints "Done" if you end the loop at around 17 but crashes with 20. Exact number might be different for you.

推荐答案

此代码看起来正确。它听起来像你遇到了编译器中的错误,这通常是在发布配置崩溃但不是调试的情况下。您可以通过在调试生成和测试中启用优化来查看它是否生成问题来进行验证。除了解决方法外,您唯一需要做的其他事情是提交错误

This code looks correct. It sounds like you've run across a bug in the compiler, which is generally the case when you have a crash in release configuration but not debug. You can perhaps verify this by enabling optimizations in your debug build and testing to see if it generates the problem. Aside from your workaround, the only other thing you need to do is file a bug.

这篇关于Swift 2.1错误排序到位,只有在发行版本上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆