ios - How to declare a generic protocol property requirement in a protocol -
struggling while with, helpful if can shed light on this:
i have apiworkerprotocol has property requirement, required property protocol i.e dataparserprotocol
protocol apiworkerprotocol { var apirequestprovider : apirequestgeneratorprotocol {get} var dataparser : dataparserprotocol{get} func callapi(completionhandler: @escaping (apicallresult<self.resulttype>) -> void) } protocol dataparserprotocol { associatedtype expectedrawdatatype associatedtype resulttype func parsefetcheddata(fetcheddata : expectedrawdatatype) -> apicallresult<resulttype> } how can achieve this?
in current implementation, causes error protocol 'dataparserprotocol' can used generic constraint because has self or associated type requirements.
thanks in advance
ankit
if protocol make use of self or associated type requirements (a homogenous protocol), may note use protocol concrete type.
so instead of using dataparserprotocol concrete type of dataparser property (blueprinted in apiworkerprotocol), add associatedtype typeholder, dataparser, apiworkerprotocol, is constrained types conform to dataparserprotocol.
also, i'm not sure intent using self.resulttype specialization in completion handler of callapi(...) (since self refer type implementing apiworkerprotocol; protocol blueprints no associatedtype resulttype): did mean use resulttype of dataparserprotocol type?
e.g.
protocol apiworkerprotocol { associatedtype dataparser: dataparserprotocol var dataparser : dataparser { } func callapi(completionhandler: @escaping (apicallresult<dataparser.resulttype>) -> void) } protocol dataparserprotocol { associatedtype expectedrawdatatype associatedtype resulttype func parsefetcheddata(fetcheddata: expectedrawdatatype) -> apicallresult<resulttype> }
Comments
Post a Comment