美文网首页
2023-09-21 swift因后台数据返回类型不一致导致实体

2023-09-21 swift因后台数据返回类型不一致导致实体

作者: nickNic | 来源:发表于2023-10-27 10:24 被阅读0次
    class DJStockPoolDragonHeadItemsModel: Codable {
    
    var isBusiness:Int?
    var blockName:String?
    //入选价格
    var price:Double?
    
    var id: Int?
    var blockMarket:Int?
    //股票名字
    var name:String?
    
    //是否是龙头
    var isCock:Int?
    //入选时间
    var time: String?
    var smallMarket:Int?
    var stockStatusImg:String?
    //股票池名称
    var typeName:String?
    var market:Int?
    var blockCode:String?
    //股票代码
    var code:String?
    var customDate:int?
    var blockID:String?
    //最高涨幅
    var maxRise:Double?
    var isBsiness:Int?
    }
    

    market 字段后台起先返回的是int,突然有一天返回了string,导致于IOS这边拿不到model数据经过排查查看解析是用swift提供的解析

        if let objArr = obj as? [[String:Any]] {
            let models = objArr.toObjct(type: [DJStockPoolDragonHeadItemsModel].self)
            recvHandler(models)
        } else {
              recvHandler(nil)
        }
    
      /// 转model
      /// - Parameter type: type
      /// - Returns: model
     func toObjct<T>(type:T.Type)  -> T? where T : Decodable {
          if let obj = try? JSONSerialization.data(withJSONObject: self, options: .prettyPrinted) {
              let decoder =  JSONDecoder()
              if let data = try? decoder.decode(T.self, from: obj) {
                  return data
              }
          }
          return nil
      }
    为了弄清原因将代码修改如下
    /// 转model
      /// - Parameter type: type
      /// - Returns: model
      func toObjct<T>(type:T.Type)  -> T? where T : Decodable {
          if let obj = try? JSONSerialization.data(withJSONObject: self, options: .prettyPrinted) {
              let decoder =  JSONDecoder()
              if let data = try? decoder.decode(T.self, from: obj) {
                  return data
              }
              var data:Any?
              do {
                  data = try! decoder.decode(T.self, from: obj)
              } catch  {
                  print(error)
              }
          }
          return nil
      }
    运行后查看
    Fatal error: 'try!' expression unexpectedly raised an error: Swift.DecodingError.typeMismatch(Swift.Int, Swift.DecodingError.Context(codingPath: [_JSONKey(stringValue: "Index 0", intValue: 0), CodingKeys(stringValue: "customDate", intValue: nil)], debugDescription: "Expected to decode Int but found a string/data instead.", underlyingError: nil)): file _Test/ArrayExtensions.swift, line 44
    2023-10-28 10:12:31.679135+0800 -Test[4259:80177] Fatal error: 'try!' expression unexpectedly raised an error: Swift.DecodingError.typeMismatch(Swift.Int, Swift.DecodingError.Context(codingPath: [_JSONKey(stringValue: "Index 0", intValue: 0), CodingKeys(stringValue: "customDate", intValue: nil)], debugDescription: "Expected to decode Int but found a string/data instead.", underlyingError: nil)): file _Test/ArrayExtensions.swift, line 44
    报错信息显示 CodingKeys(stringValue: "customDate", intValue: nil)
    查看后台原始数据
    ![73AD650AE3265779FB0CC0F066EAFA2E.png](https://img.haomeiwen.com/i1192250/0d0606e6506d465f.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
    
    ![73ad650ae3265779fb0cc0f066eafa2e.png](https://img.haomeiwen.com/i1192250/c297b9b8ce92c769.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
    
    竟然是string类型,好吧我改。改完string后顺利拿到了数据
    

    相关文章

      网友评论

          本文标题:2023-09-21 swift因后台数据返回类型不一致导致实体

          本文链接:https://www.haomeiwen.com/subject/octavdtx.html