【发布时间】:2023-04-07 14:07:01
【问题描述】:
我正在 Swift 中迈出第一步,并解决了第一个问题。我正在尝试在具有约束的通用函数上使用inout 通过引用传递数组。
首先,我的应用起点:
import Foundation
let sort = Sort()
sort.sort(["A", "B", "C", "D"])
我的班级有实际问题:
import Foundation
class Sort {
func sort<T:Comparable>(items:[T]){
let startIndex = 0
let minIndex = 1
exchange(&items, firstIndex: startIndex, secondIndex: minIndex)
}
func exchange<T:Comparable>(inout array:[T], firstIndex:Int, secondIndex:Int) {
// do something with the array
}
}
我在 Xcode 中调用exchange 时遇到以下错误:
Cannot convert value of type '[T]' to expected argument type '[_]'
我在这里错过了什么吗?
更新:添加了完整的项目代码。
【问题讨论】:
-
也许发布给出错误的实际代码....
-
我添加了完整的项目代码。交换函数实际上并没有对数组做任何事情,所以这是最小的例子。