简体   繁体   English

class 方法要求 'AFDataResponse<x> ' 符合 Encodable &amp; Encodable</x>

[英]class method requires that 'AFDataResponse<X>' conform to Encodable & Encodable

I've been reading many questions on here, but no answer helped me resolve this error, my struct is Codable and is only using data types like String, Int and Double.我在这里阅读了很多问题,但没有答案帮助我解决了这个错误,我的结构是 Codable 并且只使用像 String、Int 和 Double 这样的数据类型。

I have them in separate files我将它们放在单独的文件中

struct Feature : Codable {
    var property: Properties?
    var geometry: Geometry?
}

struct Properties : Codable {
    let pk: Int
    var title: String = ""
    var name: String = ""
    //var added: Date
}

struct Geometry : Codable {
    var type: String = ""
    var coordinates: [Double]?
}

This is where I get the error这是我得到错误的地方

CommunicationManager.request(endpoint: "url", onSuccess: { (response: AFDataResponse<Feature>) in
        print(response)
    })

This is the definition of my method这是我的方法的定义

import Alamofire

typealias APIHeaders = HTTPHeaders
typealias APIParameters = [String: Any]

enum APIMethod {
    case get
    case post
    case delete
    case put

    fileprivate var value: HTTPMethod {
        switch self {
        case .get:
            return HTTPMethod.get
        case .post:
            return HTTPMethod.post
        case .delete:
            return HTTPMethod.delete
        case .put:
            return HTTPMethod.put
        }
    }
}

enum APIEncoding {
    case json
    case url

    fileprivate var value: ParameterEncoding {
        switch self {
        case .json:
            return JSONEncoding.default
        default:
            return URLEncoding.default
        }
    }
}

class APIError {
    var code: String?
    var message: String?

    init(message: String) {
        self.code = nil
        self.message = message
    }
}

class APISessionManager: Session {
    static let shared: APISessionManager = {
        let configuration = URLSessionConfiguration.default
        configuration.timeoutIntervalForRequest = 40
        configuration.requestCachePolicy = .reloadIgnoringLocalAndRemoteCacheData
        let manager = APISessionManager(configuration: configuration)
        return manager
    }()
}

class CommunicationManager {

    class func request<T: Codable>(endpoint: String,
                       method: APIMethod = .get,
                       encoding: APIEncoding = .url,
                       parameters: APIParameters? = nil,
                       headers: APIHeaders? = nil,
                       onSuccess: @escaping (T) -> Void,
                       onFailure: ((APIError)-> Void)? = nil){

        let request = APISessionManager
            .shared
            .request(endpoint,
                     method: method.value,
                     parameters: parameters,
                     encoding: encoding.value,
                     headers: headers)

        request.responseJSON(completionHandler: { response in

            guard response.error == nil else {
                onFailure?(APIError(message: "error"))
                return
            }

            guard let data = response.data else {
                onFailure?(APIError(message: "error"))
                return
            }

            guard let object = try? JSONDecoder().decode(T.self, from: data) else {
                onFailure?(APIError(message: "error"))
                return
            }

            onSuccess(object)
            print(response)

        })

    }

}

I really don't know what to do, all the questions people asked were resolved because they used Image, or some other data type that does not use the protocol Codable我真的不知道该怎么办,人们提出的所有问题都得到了解决,因为他们使用了 Image,或者其他一些不使用 Codable 协议的数据类型

In the API you've described, the onSuccess closure would take a Feature value, not an AFDataResponse<Feature> value.在您描述的 API 中, onSuccess闭包将采用Feature值,而不是AFDataResponse<Feature>值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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