When I try the following code:雨燕3.1的錯誤:無法調用「步幅」型
extension Int{
func hello(to end: Int, by step: Int, task: (Int) -> Void){
for i in stride(from: 4, to: 8, by: 2) {
task(i)
}
}
}
And I get the error saying:
error: cannot invoke 'stride' with an argument list of type '(from: Int, to: Int, by: Int)' for i in stride(from: 4, to: 8, by: 2)
note: overloads for 'stride' exist with these partially matching parameter lists: (to: Self, by: Self.Stride), (through: Self, by: Self.Stride) for i in stride(from: 4, to: 8, by: 2)
的參數列表,我不爲什麼這種類型的錯誤發生
我不能說爲什麼會出現此特定錯誤,但它工作正常,如果它不是在'Int'擴展。 – vadian
比較https://stackoverflow.com/q/39602298/2976878。在你的具體情況中,問題在於編譯器看到了被聲明爲實例成員而不是全局函數(但現在不可用)的Swift 2'stride'方法。解決方案是相同的,用模塊名稱前綴呼叫以消除歧義。 – Hamish