我正在使用XCode 7.3.1上的Swift 2.2,尝试从另一个泛型函数中调用泛型函数。
代码
class Thing1 {
let variable: SomeProtocol
init<A: SomeProtocol>(variable: A) {
self.variable = variable
self.add1(self.variable)
}
func add1<A: SomeProtocol>(stuff: A) {
let thing: Thing2 = Thing2()
thing.add2(stuff)
}
}
class Thing2 {
func add2<A: SomeProtocol>(stuff: A) {
}
}
protocol SomeProtocol { }
add1("a") // Cannot invoke 'add1' with an argument list of type '(String)'
add1(4) // Cannot invoke 'add1' with an argument list of type '(Int)'
我遇到了错误。
'Cannot invoke add with an argument of list type '(Whatever type I used to call the function)''
SomeProtocol
、Thing
等内容。 - Alexanderadd1
的类型属于SomeProtocol
?例如extension String : SomeProtocol { }
和extension Int : SomeProtocol { }
。 - dfrib