【发布时间】:2022-08-09 18:44:55
【问题描述】:
当我尝试呈现结果时,我收到了这条消息\“由于错误,无法解码响应: 无法读取数据,因为它的格式不正确。\"
这是我的格式,我认为它是正确的。
import Foundation
// MARK: - Response
struct Response: Codable {
let code: Int
let meta: Meta
let data: [Datum]
}
// MARK: - Datum
struct Datum: Codable {
let id, userID: Int
let title, body: String
enum CodingKeys: String, CodingKey {
case id
case userID = \"user_id\"
case title, body
}
}
// MARK: - Meta
struct Meta: Codable {
let pagination: Pagination
}
// MARK: - Pagination
struct Pagination: Codable {
let total, pages, page, limit: Int
}
我也尝试使用此代码查看结果。
private func fetchData() {
self.task = AF.request(self.baseUrl, method: .get, parameters: nil)
.publishDecodable(type: [Response].self)
.sink(receiveCompletion: {(completion) in
switch completion {
case .finished:
()
case .failure(let error):
print(String(describing: error))
//print(error.localizedDescription)
}
}, receiveValue: {[weak self ](response) in
switch response.result {
case .success(let model): self?.presenters = model.map {PostPresenter(with: $0)}
case.failure(let error):
print(String(describing: error))
// print(error.localizedDescription)
}
})
}
我的帖子演示者代码是这个
struct PostPresenter: Identifiable {
let id = UUID()
let title: String
init(with model:Response) {
self.title = model.data
}
}
-
完整的错误信息是什么?
-
Xcode 错误 - 我无法将类型 \'[Datum]\' 的值分配给类型 \'String\' 好的,我们已经理解了数组。输出是在 UIViewAlertForUnsatisfiableConstraints 处创建一个符号断点以在调试器中捕获它。 <UIKitCore/UIView.h> 中列出的 UIView 上的 UIConstraintBasedLayoutDebugging 类别中的方法也可能会有所帮助。 2022-07-18 16:12:53.951091+0300 iOS 作业 [17805:373050] [boringssl]boringssl_metrics_log_metric_block_invoke(153) 未能记录指标 由于错误而无法解码响应:无法读取数据,因为它不是t 格式正确。
-
@kakouliadis 为了便于阅读,请不要写在 cmets 中,请 edit 您的问题提供更多信息。
标签: json swift alamofire codable jsondecoder