Reputation: 13281
It's such a shame that I really have no idea with this. I have this project that was developed by someone else and it's using TRON library.
My question is pretty straightforward. How do I conform to a protocol with an associatedtype
. I know how to write a protocol and conform to it just like with UITableViewDataSource
and UITableViewDelegate
.
Here's the code of TRON's protocol:
Serializer.swift
import Foundation
import Alamofire
/// The type in which all data and upload response serializers must conform to in order to serialize a response.
public protocol ErrorHandlingDataResponseSerializerProtocol : DataResponseSerializerProtocol {
/// The type of serialized object to be created by this `ErrorHandlingDataResponseSerializerProtocol`.
associatedtype SerializedError
/// A closure used by response handlers that takes a parsed result, request, response, data and error and returns a serialized error.
var serializeError: (Alamofire.Result<SerializedObject>?,URLRequest?, HTTPURLResponse?, Data?, Error?) -> APIError<SerializedError> { get }
}
And I'm trying to conform to that protocol like so:
class CustomErrorHandlingSerializer: ErrorHandlingDataResponseSerializerProtocol {
}
I need to conform to that because there is a function here that needs an ErrorHandlingDataResponseSerializerProtocol
,
I have tried reading the library's documentation and even their migration guide. It's either they really do not have a straightforward example on their usage OR I just don't understand their documentation.
Upvotes: 0
Views: 307
Reputation: 631
TRON author here.
Most of the time you don't need to implement this protocol, because responseSerializer is provided by default, if your models are JSONDecodable or Codable. I assume you were using older version of TRON with SwiftyJSON, in that case you would probably need to call request in this way:
let request : APIRequest<...,...> = tron.swiftyJSON.request("path")
If you really want to implement custom response serializer, you would basically need to implement two protocols - ErrorHandlingDataResponseSerializerProtocol
and DataResponseSerializerProtocol
, which both contain one associatedtype and a variable.
You can use typealias, but it's better to let Swift infer associatedtype from variable signature. You can take a look at Response Serializers doc for reference, i updated it to latest syntax.
Upvotes: 1
Reputation: 17050
You need to specify what type of object will be returned by the serializeError
property. There are two ways to do it:
Define a SerializedError
subtype of your class, or:
Use typealias
to point SerializedError
to the appropriate type that your serializeError
property will provide.
Upvotes: 1