NSInvalidArgumentException, reason: 'Invalid type in JSON write (__NSDate)'

前端 未结 8 2163
南旧
南旧 2021-02-04 01:05

I\'m getting this exception when I try to JSON encode NSDate object.I believe NSDate is not compatible for JSON encoding. but I must encode the date.Any solutions?



        
8条回答
  •  深忆病人
    2021-02-04 01:30

    I am doing something like this in my body params encoder

    // handle dates
                  var _params = [String: Any]()
                  params.forEach { (key, value) in
                    if let date = value as? Date {
                        _params[key] = DateFormatter.iso8601Full.string(from: date)
                    } else {
                        _params[key] = value
                    }
                  }
    
                  return try? JSONSerialization.data(withJSONObject: _params)
    

提交回复
热议问题