我的问题是基于 Swift 的书的 Guided Tour 章节,其中给出了以下代码:
func anyCommonElements <T, U where T: Sequence, U: Sequence,
T.GeneratorType.Element: Equatable,
T.GeneratorType.Element == U.GeneratorType.Element>
(lhs: T, rhs: U) -> Bool
{
for lhsItem in lhs {
for rhsItem in rhs {
if lhsItem == rhsItem {
return true
}
}
}
return false
}
anyCommonElements([1, 2, 3], [3])
这本书要求尝试修改函数以返回一组公共元素,而我能够做到。
但是我开始尝试尝试修改函数以返回另一个构建数组的函数,这就是我遇到的问题。
这是我所拥有的:
func myCommonElements<T, U where T: Sequence, U: Sequence,
T.GeneratorType.Element: Equatable,
T.GeneratorType.Element == U.GeneratorType.Element>
(lhs: T) -> (U -> Array<U.GeneratorType.Element>)
{
func makeCommon (rhs: U) -> Array<U.GeneratorType.Element>
{
var commonArray = Array<U.GeneratorType.Element>()
for lhsItem in lhs {
for rhsItem in rhs {
if lhsItem == rhsItem {
commonArray.append(lhsItem)
}
}
}
return commonArray
}
return makeCommon
}
let gatherCommon = myCommonElements([3, 4, 5, 6])
let result = gatherCommon([1, 2, 3, 4])
println(result)
我得到的错误是:
cannot convert the expression's type
'(rhs: $T2 -> Array<$T4>)' to type 'Generator'
我了解该错误,但我不确定为什么会收到此错误。我做错了什么?