You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have a structure called OTKSession that conforms to JSONDecodable, however using in it context of Array within generics does not work and I get the following error:
Client.swift:128:19: In argument type '(Result<[OTKSession]>) -> Void', '[OTKSession]' does not conform to expected type 'JSONDecodable'
Example code:
func rest<ResponseType:JSONDecodable>(_ completionHandler:@escaping(Result<ResponseType>)->Void){letvalue=...// comes asynchronouslyletjsonString=String(bytes: value.0, encoding:.utf8)!
letresponseObject=try!ResponseType(JSONString: jsonString)completionHandler(.ok(responseObject))}
Do I have to handle arrays separately or it is possible to incorporate them within single function? It seems like there is no way to constraint generics to Array of JSONDecodable objects.
At the moment I've added overloaded method that wraps ResponseType in array. Two methods coexist just fine but it seems like a code duplication to me which probably can be solved in more elegant way.
func rest<ResponseType:JSONDecodable>(_ completionHandler:@escaping(Result<[ResponseType]>)->Void){letvalue=...// comes asynchronouslyletjsonString=String(bytes: value.0, encoding:.utf8)!
letresponseObject=try![ResponseType](JSONString: jsonString)completionHandler(.ok(responseObject))}
The text was updated successfully, but these errors were encountered:
Hi thanks for asking, but unfortunately I do not have a good answer to this.. In fact we are handling similar cases like this within the JSONCodable library itself.
I have a structure called
OTKSession
that conforms toJSONDecodable
, however using in it context ofArray
within generics does not work and I get the following error:Example code:
Do I have to handle arrays separately or it is possible to incorporate them within single function? It seems like there is no way to constraint generics to Array of JSONDecodable objects.
At the moment I've added overloaded method that wraps ResponseType in array. Two methods coexist just fine but it seems like a code duplication to me which probably can be solved in more elegant way.
The text was updated successfully, but these errors were encountered: