Why is compiler forcing me to use sorted(by:) in Swift 3

六眼飞鱼酱① 提交于 2019-12-12 03:43:20

问题


I am using Swift 3. I have a dictionary of arrays, and I am trying to sort the arrays in place using a property each object in the array contains.

As far as I understand it as of Swift 3 sorted() does not sort in place, but instead returns a sorted array value. If you want to sort in place you should use sort(). But when I try the compiler keeps saying "sort() has been renamed sorted(by:)

Why won't the compiler let me use sort()

Here is my code:

func sortAllArraysInDict() {

    for arrayOfGrowthPaths in catDict.values {

        arrayOfGrowthPaths.sort({$0.growthPathDisplayOrder < $1.growthPathDisplayOrder})
}

回答1:


Make sure you declare your array as variable and just delete the parentheses (trailing closure syntax):

arrayOfGrowthPaths.sort { $0.growthPathDisplayOrder < $1.growthPathDisplayOrder }


来源:https://stackoverflow.com/questions/41295721/why-is-compiler-forcing-me-to-use-sortedby-in-swift-3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!