【发布时间】:2016-06-14 07:12:48
【问题描述】:
我在 Swift 中编译测试用例时遇到问题。看起来编译器正在丢失有关模板类型的信息,但其他通用方法工作正常。我错过了什么?
public class MatchNorm {
public static func resolve1<T:SequenceType where T.Generator.Element:MatchNormElement>(list: T, lti: LinearTransformation, accuracy: Double) -> LinearTransformation {
// no problem
return MatchNorm.resolve1(list, lti: lti, accuracy: accuracy)
}
public static func resolve2<T:SequenceType where T.Generator.Element:MatchNormElement>(list: T, lti: LinearTransformation, accuracy: Double) -> LinearTransformation {
for elem in list {
print(elem.x)
}
return lti
}
}
public class MatchNormTest: XCTestCase {
func testMatchNorm1() {
var list = [MatchNormElement]()
// compilation error here!
let ll = MatchNorm.resolve1(list, lti: LinearTransformation(1), accuracy: 0.001)
// MatchNormTest.swift:70:29: Cannot invoke 'resolve1' with an argument list of type '([MatchNormElement], lti: LinearTransformation, accuracy: Double)'
// MatchNormTest.swift:70:29: Expected an argument list of type '(T, lti: LinearTransformation, accuracy: Double)'
}
}
更新
MatchNormElement 是一个协议,所以我将其更改为具体类型。现在可以了。
func testMatchNorm1() {
var list = [Measurment]()
// works fine
let ll = MatchNorm.resolve1(list, lti: LinearTransformation(1), accuracy: 0.001)
}
【问题讨论】:
-
我用
NSString替换了MatchNormElement和LinearTransformation(并删除了resolve2,这个方法不需要找问题),没有编译错误。也许这些信息会对您有所帮助。 -
感谢 ShadowOf。 MatchNormElement 是一个协议。看起来列表必须是具体类型。
标签: xcode swift generics xctest