【问题标题】:Swift Rest API Call with Codable returning array带有可编码返回数组的 Swift Rest API 调用
【发布时间】:2021-01-26 05:11:03
【问题描述】:

这是关于这个问题的进一步链接 Swift Rest API call example using Codable

当我更改代码以处理数组时,我收到 JSON 错误。 这就是我正在做的事情。 我究竟做错了什么?它与“json 错误”出错。

// Transaction Class

import Foundation

final class Transaction: Codable {
    
    var id: Int?
    var date: Date
    var amount: Int
    var planeID: Int
    var userID: UUID
    var month: Int
    var year: Int

    
    init(date: Date, amount: Int, planeID: Int, userID: UUID, month: Int, year: Int) {
        self.date = date
        self.amount = amount
        self.planeID = planeID
        self.userID = userID
        self.month = month
        self.year = year
    }
}

// Call in my ViewController

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        
        getJson() { (json) in
            print(json.count)
            //print(json[0].note)
            print(json)
        }
    }
    
    func getJson(completion: @escaping ([Transaction])-> ()) {
        let urlString = "http://192.168.1.98:8080/admin/transactions"
        if let url = URL(string: urlString) {
        URLSession.shared.dataTask(with: url) {data, res, err in
            guard let data = data else {return print("error with data")}
            let decoder = JSONDecoder()
            guard let json = try? decoder.decode([Transaction].self, from: data) else {return print("error with json")}
            completion(json)
          }.resume()
        }
    }

}

这是我调用 Rested 时的 JSON 响应正文

//Form of the JSON response body when I used call via Rested

[
    {
        "amount": 1000,
        "userID": "7698E011-6643-421E-A30D-121FF488FDBB",
        "id": 2,
        "month": 5,
        "date": "2021-01-14T20:30:31Z",
        "year": 2021,
        "planeID": 1
    },
    {
        "amount": 1000,
        "userID": "7698E011-6643-421E-A30D-121FF488FDBB",
        "id": 3,
        "month": 5,
        "date": "2021-01-14T20:30:31Z",
        "year": 2021,
        "planeID": 2
    },
    {
        "amount": 1000,
        "userID": "7698E011-6643-421E-A30D-121FF488FDBB",
        "id": 4,
        "month": 5,
        "date": "2021-01-14T20:30:31Z",
        "year": 2021,
        "planeID": 2
    },
    {
        "amount": 1000,
        "userID": "7698E011-6643-421E-A30D-121FF488FDBB",
        "id": 5,
        "month": 5,
        "date": "2021-01-14T20:30:31Z",
        "year": 2021,
        "planeID": 2
    },
    {
        "amount": 1000,
        "userID": "7698E011-6643-421E-A30D-121FF488FDBB",
        "id": 6,
        "month": 5,
        "date": "2021-01-14T20:30:31Z",
        "year": 2021,
        "planeID": 2
    }
]

【问题讨论】:

  • 不要try? decoder,添加do - catch 块并打印error 实例。 DecodingErrors 非常具有描述性。并且也不要打印无意义的"error with data",打印真正的error
  • 您在上一个问题中接受的答案非常糟糕,您应该那样处理错误。

标签: swift rest codable


【解决方案1】:

如果您使用try/catch 并打印错误,您会看到在解码date 属性时发生错误。 (如 cmets 中所述,不要使用 try? 隐藏错误 - 你不会知道出了什么问题)

要将日期正确解码为Date,需要在解码器上设置日期解码策略。在您的情况下,日期似乎是标准 ISO8601,这是JSONDecoder 中的内置解码策略。因此,您只需添加一行:

let decoder = JSONDecoder()
decoder.dateDecodingStrategy = .iso8601 // <-- ADD THIS

// use do/try/catch with decoding
do {
   let transactions = try decoder.decode([Transaction].self, from: data)
   completion(transactions)
} catch {
   print(error) // or handle it somehow
}

【讨论】:

  • @dreadbot,不确定你做了什么得到一个 nil 错误。
  • 谢谢,API 和我的课程不匹配。
猜你喜欢
  • 2016-06-13
  • 2019-10-21
  • 1970-01-01
  • 2019-05-18
  • 1970-01-01
  • 1970-01-01
  • 2013-06-03
  • 2020-07-04
  • 1970-01-01
相关资源
最近更新 更多