不确定在哪里做什么;我相信我的类型符合可解码要求!
let dm = DataManager(networkManagers: [mockHTTPManager])
let ep = Endpoint(scheme: .http, host: "api.nytimes.com",path: "/search/repositories")
dm.object(from: ep, with: DisplayContent.self) {result in
print (result)
}
其中DisplayContent是结构
struct DisplayContent:Decodable {
var title: String?
var abstract: String?
var thumbnailImageString: String?
var date: String?
var image: String?
}
并且我正在尝试创建一种将数据一般转换为对象的方法,但我认为此处仅涉及单数
func object<T : Decodable>(from endpoint: Endpoint, with object: T, completion: @escaping (Result<T, Error>) -> Void) {
let error = NSError(domain:"", code:-1009, userInfo:[ NSLocalizedDescriptionKey: "Internet Offline"]) as Error
let url = endpoint.url!
networkManagers.first!.get(url: url) { result in
switch result {
case .failure: print ("failure")
case .success(let success) :
do {
let decoder = JSONDecoder()
let content = try decoder.decode(T.self, from: success)
print ("content")
} catch {
}
}
}
}
错误是“参数类型'DisplayContent.Type'不符合预期的类型'Decodable'”,但DisplayContent符合可解码性!
答案 0 :(得分:1)
您的函数期望一个类型为T(with object: T
)的对象,但是您正在尝试传递一个 type (DisplayContent.self
)。 / p>
您可以:
DisplayContent()
),或with object: T.type
)。