Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios I';我得到了一个错误,预期解码Int,但在swift上发现了一个字符串/数据_Ios_Json_Swift_Type Mismatch - Fatal编程技术网

Ios I';我得到了一个错误,预期解码Int,但在swift上发现了一个字符串/数据

Ios I';我得到了一个错误,预期解码Int,但在swift上发现了一个字符串/数据,ios,json,swift,type-mismatch,Ios,Json,Swift,Type Mismatch,我不明白为什么我会犯这个错误。控制台说我将代码声明为字符串,但实际上是Int。有什么帮助吗 我得到的确切错误信息是: DEBUG:发生了一些问题,无法获取数据:responseSerializationFailed(原因:Alamofire.AFError.responseSerializationFaileureReason.decodingFailed(错误:Swift.DecodingError.typeMismatch(Swift.Int,Swift.DecodingError.Cont

我不明白为什么我会犯这个错误。控制台说我将代码声明为字符串,但实际上是Int。有什么帮助吗

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

DEBUG:发生了一些问题,无法获取数据:responseSerializationFailed(原因:Alamofire.AFError.responseSerializationFaileureReason.decodingFailed(错误:Swift.DecodingError.typeMismatch(Swift.Int,Swift.DecodingError.Context)(编码路径:[CodingKeys(stringValue:,intValue:nil)],debugDescription:“应解码Int,但找到了字符串/数据。”,underyingerror:nil))

DEBUG:API调用出现错误:responseSerializationFailed(原因:Alamofire.AFError.responseSerializationFaileureReason.decodingFailed(错误:Swift.DecodingError.typeMismatch(Swift.Int,Swift.DecodingError.Context)(编码路径:[CodingKeys(stringValue:“code”,intValue:nil)],debugDescription:“应解码Int,但找到了字符串/数据。“,underyingerror: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模式是:

我的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()

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

失败的JSON是什么样子的?这在JSON模式图上。这意味着
代码
不是它应该是的
Int
。您打印了错误,但是您是否也可以在
catch
print(“responseString失败”(String(data:response.data,encoding:.utf8)??“”)中编辑您的问题,并发布用户返回的实际JSON字符串API@3rnestocs上面的意思是,你能给我们发送一个原始JSON字符串,用于
JSONDecoder().decode吗(Response.self,from:Response.data)
。您可以在使用
print(字符串(数据:Response.data,编码:.utf8)?“无响应”)对实际模型进行解码之前执行此操作。