我找到了这些答案:
在swift中对元组数组 进行排序 3 如何对 元组数组 进行排序?
但我还有问题。 这是我的代码:
var countsForLetter:[(count:Int, letter:Character)] = [] ... countsForLetter.sorted(by: {$0.count < $1.count})Swift 3希望我添加by:现在它说,调用sorted:by的结果是未使用的。
我是新来的swift 3.对不起,如果这是一个基本的问题。
I found these answers:
Sort an array of tuples in swift 3 How to sort an Array of Tuples?
But I'm still having issues. Here is my code:
var countsForLetter:[(count:Int, letter:Character)] = [] ... countsForLetter.sorted(by: {$0.count < $1.count})Swift 3 wanted me to add the by: and now it says that the result of the call to sorted:by is unused.
I'm new to swift 3. Sorry if this is a basic question.
最满意答案
你得到这个警告是因为sorted(by...返回一个新的排序后的数组版本,所以这个警告指出你没有把它分配给一个变量或任何东西,你可以说:
countsForLetter = countsForLetter.sorted(by: {$0.count < $1.count})我怀疑你试图“排序”,所以在这种情况下,你可以改变sorted(by sort(by它,它只是对countsForLetter排序并将其分配给相同的变量。
You are getting that warning because sorted(by... returns a new, sorted version of the array you call it on. So the warning is pointing out the fact that you're not assigning it to a variable or anything. You could say:
countsForLetter = countsForLetter.sorted(by: {$0.count < $1.count})I suspect that you're trying to "sort in place", so in that case you could change sorted(by to sort(by and it would just sort countsForLetter and leave it assigned to the same variable.
更多推荐
发布评论