简体   繁体   English

类型不匹配(Swift.Dictionary<swift.string, any> ,</swift.string,>

[英]typeMismatch(Swift.Dictionary<Swift.String, Any>,

my JSON:我的 JSON:

'''
{
  "status": 200,
  "data": {
    "widgets": [
      {
        "type": "small_half_slider",
        "data": {
          "banners": [
            {
              "id": 42765,
              "image": "",
              "title": "!",
              "is_ad": false,
              "url": {
                "url": "",
                "page": "web_link",
                "params": {
                  "url": ""
                },
                "queries": []
              },
              "video": "",
              "description": "",
              "bullet_points": []
            }
'''

and my Struct:和我的结构:

'''
import Foundation

struct mainPage:Codable{
  var  status:Int?
  var  data: mData?
}

struct mData:Codable {
    var widgets:[Mwidg]
}

struct Mwidg:Codable{

    var type:String
    var data:wdata
}

struct wdata:Codable{
   var show_type:String?
   var banners:[bannerData]?
}

struct bannerData:Codable {
    var id:Int?
    var image:String?
    var title:String?
    var is_ad:Bool?
    var url:urlData?
    var video:String?
    var description:String?

}
struct urlData:Codable {
    var url:String?
    var page:String?
    var params:paramsData?
    var queries:[queriesData]?
}  

struct paramsData:Codable {
    var url:String?
}

struct queriesData:Codable {

 }


'''

then i faced to this error:然后我遇到了这个错误:

typeMismatch(Swift.Dictionary<Swift.String, Any>, Swift.DecodingError.Context(codingPath: [CodingKeys(stringValue: "data", intValue: nil), CodingKeys(stringValue: "widgets", intValue: nil), _JSONKey(stringValue: "Index 1", intValue: 1), CodingKeys(stringValue: "data", intValue: nil)], debugDescription: "Expected to decode Dictionary<String, Any> but found an array instead.", underlyingError: nil)) typeMismatch(Swift.Dictionary<Swift.String, Any>, Swift.DecodingError.Context(codingPath: [CodingKeys(stringValue: "data", intValue: nil), CodingKeys(stringValue: "widgets", intValue: nil), _JSONKey( stringValue: "Index 1", intValue: 1), CodingKeys(stringValue: "data", intValue: nil)], debugDescription: "期望解码 Dictionary<String, Any> 但找到了一个数组。",underlyingError: nil) )

You are facing this issue because in your json response you are not getting values or getting nil values for properties which are present in struct mData and Mwidg, to work your solution make struct properties as optional so that even if you didn't receive data in your json response the code will work.您正面临这个问题,因为在您的 json 响应中,您没有获取值或获取 struct mData 和 Mwidg 中存在的属性的 nil 值,以便您的解决方案将结构属性设为可选,这样即使您没有收到数据您的 json 响应代码将起作用。 The json you pasted in question is not completed and it may possible that you were getting some object as nil.您粘贴的 json 尚未完成,您可能会得到一些 object 为零。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 类型不匹配(Swift.Dictionary<Swift.String, Any> debugDescription: &quot;预期解码字典<String, Any>而是找到了一个数组。” - typeMismatch(Swift.Dictionary<Swift.String, Any> debugDescription: "Expected to decode Dictionary<String, Any> but found an array instead." 无法转换类型为&#39;Swift.Array的值 <Swift.Dictionary<Swift.String, Swift.String> &gt;”()到“ Swift.Dictionary” <Swift.String, Any> &#39;() - Could not cast value of type 'Swift.Array<Swift.Dictionary<Swift.String, Swift.String>>' () to 'Swift.Dictionary<Swift.String, Any>' () 无法转换类型“Swift.Array”的值<Any> &#39; 到 &#39;Swift.Dictionary<Swift.String, Any> &#39; - Could not cast value of type 'Swift.Array<Any>' to 'Swift.Dictionary<Swift.String, Any>' “unsafeMutableAddressor : Swift.String”,引用自:...” - "unsafeMutableAddressor : Swift.String", referenced from:.." 无法转换类型为&#39;Swift._SwiftDeferredNSDictionary&#39;的值 <Swift.String, Swift.String> ”到“ NSMutableDictionary” - Could not cast value of type 'Swift._SwiftDeferredNSDictionary<Swift.String, Swift.String>' to 'NSMutableDictionary' 无法将“SwiftyJSON.JSON”类型的值转换为“Swift.String” - Could not cast value of type 'SwiftyJSON.JSON' to 'Swift.String' 存档iOS App时出错-“通用专业化 <Swift.String> Swift.String.init” - Error when archiving iOS App - “generic specialization <Swift.String> of Swift.String.init” 在Swift中将任何字典数组转换为String - Convert Any dictionary array to String in Swift 类型不匹配(Swift.Array<any> , Swift.DecodingError.Context(codingPath: [CodingKeys(stringValue: "product", intValue: nil)],</any> - typeMismatch(Swift.Array<Any>, Swift.DecodingError.Context(codingPath: [CodingKeys(stringValue: "product", intValue: nil)], Swift Codable:无法解码 [String: Any] 或 [String: Decodable] 类型的字典 - Swift Codable: Cannot decode dictionary of type [String: Any] or [String: Decodable]
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM