Swift 2.1 错误排序到位,仅适用于发布版本 [英] Swift 2.1 Error sorting in place, only on release builds

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

问题描述

我开始在下面的代码中获取 sort 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--------------------

好的,这是一个崩溃的最小版本.原来 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.
}


}

所以在 release 中运行它,如果你在 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天全站免登陆