【问题标题】:I'm getting the error Expected to decode Int but found a string/data instead on swift我收到错误 Expected to decode Int but found a string/data instead on swift
【发布时间】:2020-11-23 16:02:01
【问题描述】:

我无法弄清楚为什么会出现此错误。控制台说我将代码声明为字符串,但它实际上是一个 Int。有什么帮助吗?

我得到的确切错误信息是这样的:

DEBUG: Something happened and couldn't fetch the data: responseSerializationFailed(reason: Alamofire.AFError.ResponseSerializationFailureReason.decodingFailed(error: Swift.DecodingError.typeMismatch(Swift.Int, Swift.DecodingError.Context(codingPath: [CodingKeys(stringValue: "code", intValue: nil)], debugDescription: "Expected to decode Int but found a string/data instead.", underlyingError: nil))))

DEBUG: There was an error with the API calling: responseSerializationFailed(reason: Alamofire.AFError.ResponseSerializationFailureReason.decodingFailed(error: Swift.DecodingError.typeMismatch(Swift.Int, Swift.DecodingError.Context(codingPath: [CodingKeys(stringValue: "code", intValue: nil)], debugDescription: "Expected to decode Int but found a string/data instead.", underlyingError: nil))))

我的结构:

struct Response: Decodable {

let code: Int?
let status: String?
let copyright: String?
var data: MarvelData?
}

编辑 1: 我的结构的其余部分。

struct MarvelData: Decodable {

let count: Int?
var limit: Int?
let offset: Int?
let results: [Characters]?
}

struct Characters: Decodable {

var id: Int?
var name: String?
var description: String?
var thumbnail: Images?
}

JSON 架构是: JSON Schema

我的 API 调用是:

AF.request(baseURL, parameters: ["apikey": publicKey,
                                     "ts" : ts,
                                     "hash": hash]).responseMarvel { (response) in

        if let error = response.error {
            
            print("DEBUG: Something happened and couldn't fetch the data: \(error)")
            handler(.failure(error))
        }
                                        
        do {
            let marvelFetch = response.value
            let results     = marvelFetch?.data?.results
            
            guard let marvelStuff = results as [Characters]? else { return }
            
            characterArray = marvelStuff

            handler(.success(characterArray))
        } catch {
            print("DEBUG: You had an error creating JSON: \(error)")
        }
   }.resume()

【问题讨论】:

  • 失败的 JSON 是什么样子的?
  • 在 JSON Schema 图片上
  • 这意味着code 不是应有的Int。你打印了错误,但你也可以在catch print("It failed with responseString "(String(data: response.data, encoding: .utf8) ?? "")")`中做
  • 编辑您的问题并发布 API 返回的实际 JSON 字符串
  • @3rnestocs 上面的人的意思是,您能否向我们发送您用于JSONDecoder().decode(Response.self, from: response.data) 的原始 JSON 字符串。您可以在使用 print(String(data: response.data, encoding: .utf8) ?? "No reponse") 解码为实际模型之前完成此操作@

标签: ios json swift type-mismatch


【解决方案1】:

这里的错误信息很清楚。这意味着在 Response 结构中使用的变量“code”被声明为 Int,但它在 JSON 响应中的类型是 String。

【讨论】:

    猜你喜欢
    • 2022-11-24
    • 1970-01-01
    • 1970-01-01
    • 2011-04-14
    • 1970-01-01
    • 1970-01-01
    • 2018-08-17
    • 2021-07-18
    • 2021-04-27
    相关资源
    最近更新 更多