“前缀的歧义使用"Swift 3 的编译器错误 [英] "Ambiguous use of prefix" compiler error with Swift 3
问题描述
我刚刚使用 Xcode 8 beta 将我的项目从 Swift 2.2 迁移到 Swift 3.0.
I just migrated my project from Swift 2.2 to Swift 3.0 with Xcode 8 beta.
我有类似于以下代码的内容(您可以将其粘贴到 Playground 中):
I have something similar to the following code (you can paste this into a 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 时出现错误
In Swift 2.2 this compiled fine. However, with Swift 3.0 I get the error
'前缀'的歧义使用
让它在 Swift 3.0 中编译的唯一方法是将地图拆分成单独的一行:
The only way to get this to compile in Swift 3.0 is to split out the map into a separate line:
let sortedDatesWithCount = datesWithCount.sorted {
$0.0 < $1.0
}.prefix(1)
let mappedDates = sortedDatesWithCount.map {
return $0.0
}
顺便说一句,在实际代码中,我从 map
返回 NSNotification
对象,而不是 Date
,但错误是相同的.我只是在这里使用了 Date
来简化示例.
BTW, in the actual code I'm returning NSNotification
objects from the map
not Date
s but the error is the same. I just used Date
here for making the example simple.
有什么办法可以把它编译成单行吗?
Is there any way to get this to compile as a one liner?
更新:为 Swift 项目创建了一个 JIRA.
UPDATE: Created a JIRA for the Swift project.
推荐答案
如果在将 ArraySlice 传递给 map
之前将其制作成一个数组,它会起作用:
It works if you make the ArraySlice into an Array before passing it to map
:
let dates: [Date] = Array(datesWithCount.sorted {
$0.0 < $1.0
}.prefix(1)).map { return $0.0 }
这看起来是编译器中的类型推断错误.
This looks like a type inference bug in the compiler.
这篇关于“前缀的歧义使用"Swift 3 的编译器错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!