类方法要求'AFDataResponse<X>'符合Encodable&Encodable。

问题描述 投票:2回答:1

我在这里看了很多问题,但是没有答案帮助我解决这个错误,我的结构是Codable的,只使用String、Int和Double等数据类型。

我把它们放在单独的文件中

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]?
}

这就是我得到错误的地方

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

这是我的方法的定义

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)

        })

    }

}

我真的不知道该怎么办了,大家问的问题都解决了,因为他们用的是Image,或者其他不使用协议Codable的数据类型。

ios swift alamofire swift5 codable
1个回答
1
投票

在你所描述的API中 onSuccess 关闭将需要一个 Feature 值,而非 AFDataResponse<Feature> 价值。

© www.soinside.com 2019 - 2024. All rights reserved.