gpt4 book ai didi

arrays - Swift 3 对元组数组进行排序

转载 作者:IT王子 更新时间:2023-10-29 05:46:49 24 4
gpt4 key购买 nike

我找到了这些答案:

Sort an array of tuples in swift 3 How to sort an Array of Tuples?

但我仍然遇到问题。这是我的代码:

var countsForLetter:[(count:Int, letter:Character)] = []
...
countsForLetter.sorted(by: {$0.count < $1.count})

Swift 3 希望我添加 by:,现在它说调用 sorted:by 的结果未被使用。

我是 swift 3 的新手。对不起,如果这是一个基本问题。

最佳答案

您收到该警告是因为 sorted(by... 返回您调用它的数组的新排序版本。所以警告指出您没有分配的事实它到一个变量或任何东西。你可以说:

countsForLetter = countsForLetter.sorted(by: {$0.count < $1.count})

我怀疑您正在尝试“就地排序”,因此在这种情况下,您可以将 sorted(by 更改为 sort(by ,它只会排序countsForLetter 并将其分配给同一个变量。

关于arrays - Swift 3 对元组数组进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42568577/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com