简体   繁体   中英

How do I conform to a protocol with associatedtype in Swift?

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.

  1. https://github.com/MLSDev/TRON/blob/master/Docs/4.0%20Migration%20Guide.md

  2. https://github.com/MLSDev/TRON

You need to specify what type of object will be returned by the serializeError property. There are two ways to do it:

  1. Define a SerializedError subtype of your class, or:

  2. Use typealias to point SerializedError to the appropriate type that your serializeError property will provide.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM