Swift: 如何将通用协议作为另一个协议的参数传递
Swift: How to pass the Generic protocol as a parameter for another protocol
我想将通用协议作为另一个协议的参数传递。
protocol OperationDelegate {
associatedtype T
func onOperationFinished(result:Array<T>)
func onOperationError(error:OperationError, errorMessage:String)
}
protocol ResultDelegate {
func findResult(query: String,odelegate: OperationDelegate<MyOwnDataType>)
}
如何将通用协议作为另一个协议的参数传递
我收到以下错误
Protocol 'OperationDelegate' can only be used as a generic constraint because it has Self or associated type requirements
protocol OperationDelegate {
associatedtype T
func onOperationFinished(result:Array<T>)
func onOperationError(error: Error, errorMessage:String)
}
protocol ResultDelegate {
func findResult<OpDelegate: OperationDelegate>(query: String, odelegate: OpDelegate)
}
//添加了几个PlayGround作品
class ResultsFinder: ResultDelegate {
func findResult<OpDelegate>(query: String, odelegate: OpDelegate) where OpDelegate : OperationDelegate {
print(#function)
// Do some operations
let s1 = Student()
s1.name = "one"
let s2 = Student()
s2.name = "two"
let addressList = [s1,s2]
odelegate.onOperationFinished(result: addressList as! [OpDelegate.T])
}
}
还有
class ResultDisplay: OperationDelegate {
typealias T = Student
func onOperationFinished(result: [Student]) {
// Display address
print(#function)
}
func onOperationError(error: Error, errorMessage: String) {
// Display error
print(#function)
}
}
class 主要功能 {
let resultDisplay = ResultDisplay()
let finder = ResultsFinder()
func start() {
print(#function)
finder.findResult(query: "Sridhar", odelegate: resultDisplay)
}
}
let main = Mainfunction()
main.start()
我想将通用协议作为另一个协议的参数传递。
protocol OperationDelegate {
associatedtype T
func onOperationFinished(result:Array<T>)
func onOperationError(error:OperationError, errorMessage:String)
}
protocol ResultDelegate {
func findResult(query: String,odelegate: OperationDelegate<MyOwnDataType>)
}
如何将通用协议作为另一个协议的参数传递
我收到以下错误
Protocol 'OperationDelegate' can only be used as a generic constraint because it has Self or associated type requirements
protocol OperationDelegate {
associatedtype T
func onOperationFinished(result:Array<T>)
func onOperationError(error: Error, errorMessage:String)
}
protocol ResultDelegate {
func findResult<OpDelegate: OperationDelegate>(query: String, odelegate: OpDelegate)
}
//添加了几个PlayGround作品
class ResultsFinder: ResultDelegate {
func findResult<OpDelegate>(query: String, odelegate: OpDelegate) where OpDelegate : OperationDelegate {
print(#function)
// Do some operations
let s1 = Student()
s1.name = "one"
let s2 = Student()
s2.name = "two"
let addressList = [s1,s2]
odelegate.onOperationFinished(result: addressList as! [OpDelegate.T])
}
}
还有
class ResultDisplay: OperationDelegate {
typealias T = Student
func onOperationFinished(result: [Student]) {
// Display address
print(#function)
}
func onOperationError(error: Error, errorMessage: String) {
// Display error
print(#function)
}
}
class 主要功能 {
let resultDisplay = ResultDisplay()
let finder = ResultsFinder()
func start() {
print(#function)
finder.findResult(query: "Sridhar", odelegate: resultDisplay)
}
}
let main = Mainfunction()
main.start()