Swift 3.0 排序 - 传递给不带参数的调用的参数 [英] Swift 3.0 sort - Argument passed to call that takes no arguments

查看:25
本文介绍了Swift 3.0 排序 - 传递给不带参数的调用的参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在尝试将一些 Swift 2 代码移植到 Swift 3.0.

I am currently trying to port some Swift 2 code to Swift 3.0.

这是让我发疯的一行代码.

Here is one line of code that is driving me crazy.

    public private(set) var searchHistory: [SearchHistoryEntry] = [SearchHistoryEntry]() // Struct that is cComparable 
....
...
    searchHistory.sortInPlace({ $0.lastUsage.isAfter($1.lastUsage) })

这是我的 Swift 3.0 版本

This is my Swift 3.0 Version

searchHistory.sort(by:{ $0.lastUsage.isAfter($1.lastUsage) })

lastUsage 的类型为 Date

lastUsage is of Type Date

编译器抱怨以下错误消息

The Compiler complains with the following error message

传递给不带参数的调用的参数

任何想法我做错了什么?我真的不明白编译器想告诉什么.排序需要一个块,我通过了它,一切都应该没问题.

Any Ideas what I am doing wrong? I really don not understand what the compiler wants to tell. Sort takes a block and I pass it, everything should be fine.

更新

我发现了错误.Swift 将所有 NSDate 属性转换为 Date,我们在 NSDate 上获得了一个名为 isAfter 的扩展.所以编译器再也找不到 isAfter 了.编译器错误消息完全具有误导性.

I found the mistake. Swift converted all NSDate properties to Date and we got an extension called isAfter on NSDate. So the compiler could not found isAfter anymore. The compiler error message was completely misleading.

推荐答案

刚遇到同样的错误.我之前在 Swift 中的代码是:

Just got to the same error. My previous code in Swift was:

let sorted =  array.sorted {
    $0.characters.count < $1.characters.count
}

但是,它不再起作用了.看起来他们已经用 sorted(by: )

However, it is not working anymore. Looks like they've updated sorted() with arguments to sorted(by: )

以下对我有用:

let sorted = array.sorted(by: {x, y -> Bool in 
    x.characters.count < y.characters.count 
})

这篇关于Swift 3.0 排序 - 传递给不带参数的调用的参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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