【问题标题】:Present results with alamofire/Swift使用 alamofire/Swift 呈现结果
【发布时间】: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


【解决方案1】:

两个错误

  1. 根对象是一个字典,所以它是(type: Response.self)

  2. model.data[Datum] 所以声明

    struct PostPresenter: Identifiable {
    
        let id = UUID()
        let data: [Datum]
    
        init(with response: Response) {
            self.data = response.data
        }
    }
    

    并在 Codable 上下文中打印错误总是print(error), 不是print(String(describing: error)) 也不是print(error.localizedDescription)

【讨论】:

  • 谢谢您的回答。现在我有 4 个错误。 1. 无法将 '[PostPresenter]' 类型的值转换为预期的参数类型 'Binding<Data>' 2. 无法推断通用参数 'Data' 3.Initializer 'init(_:)' 要求 'Binding<Subject> ' 在内容视图 var body 中符合 'StringProtocol' 这 3 个:一些 View { List(self.viewModel.presenters) { Text($0.title) } NavigationView ... 4. 而这在 APIViewModel - 'Response' 类型的值没有成员“地图”
  • 这是另一个与您的 SwiftUI 视图相关的问题。关于 4. 我不知道 self.presenters 是什么
【解决方案2】:

我对的帖子主持人

struct PostPresenter: Identifiable {

    let id = UUID()
    let data: [Datum]

    init(with response: Response<[Datum]>) {
        self.data = response.data

    }
    
    init() {
        data = []
    }
}

我的最终格式是正确的。

struct Response<T: Decodable>: Decodable {
    let code: Int
    let meta: Meta
    let data: T
}

// MARK: - Datum
struct Datum: Decodable {
    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: Decodable {
    let pagination: Pagination
}

// MARK: - Pagination
struct Pagination: Decodable {
    let total, pages, page, limit: Int
}

最后是我的 homeview 以及我如何呈现

struct HomeView: View {
    
    @ObservedObject var viewModel = HomeAPIViewModel()
    
    var body: some View {
        
        ZStack {
            Color(.white)
            // Present the API Here
              List(self.viewModel.presenter.data, id: \.id) {
                  Text($0.title)
                }
                .padding()
        }
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-04-09
    • 1970-01-01
    • 1970-01-01
    • 2018-06-29
    • 2020-07-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多