gpt4 book ai didi

swift3 - Swift 3 的 "Ambiguous use of prefix"编译器错误

转载 作者:行者123 更新时间:2023-12-01 22:38:46 25 4
gpt4 key购买 nike

我刚刚使用 Xcode 8 beta 将我的项目从 Swift 2.2 迁移到 Swift 3.0。

我有类似于以下代码的内容(您可以将其粘贴到 Playground 中):

import Foundation

let datesWithCount: [(Date, Int)] = [(Date(), 1), (Date(), 2), (Date(), 3)]

let dates: [Date] = datesWithCount.sorted {
$0.0 < $1.0
}.prefix(1).map {
return $0.0
}

在 Swift 2.2 中编译得很好。但是,使用 Swift 3.0 我收到错误

Ambiguous use of 'prefix'

在 Swift 3.0 中编译此代码的唯一方法是将映射拆分为单独的行:

let sortedDatesWithCount = datesWithCount.sorted {
$0.0 < $1.0
}.prefix(1)

let mappedDates = sortedDatesWithCount.map {
return $0.0
}

顺便说一句,在实际代码中,我从 map 返回 NSNotification 对象,而不是 Date,但错误是相同的。我只是在这里使用了 Date 以使示例变得简单。

有什么方法可以让它编译为一个衬垫吗?

更新:创建了 JIRA对于 Swift 项目。

最佳答案

如果在将 ArraySlice 传递给 map 之前将其转换为数组,则它会起作用:

let dates: [Date] = Array(datesWithCount.sorted {
$0.0 < $1.0
}.prefix(1)).map { return $0.0 }

看起来是编译器中的类型推断错误。

关于swift3 - Swift 3 的 "Ambiguous use of prefix"编译器错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37931172/

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