Search code examples
iosswiftmoya

Not able to parse variable other than string using Moya in swift


I am working on an iOS project in Swift. I used Moya framework for API handling and parsing. It works perfectly. But when I try to parse varibles other than string it shows me en error:

"Missing argument for parameter 'transformation' in call"

Here is my mapper class

import Mapper

class MyMapperClaa:Mappable {
    var dateVariable: NSDate?

    required init(map: Mapper) throws{
        try dateVariable = map.from("date")
    }
}

Solution

  • sorry, you are using this lib: https://github.com/lyft/mapper. from example there:

    private func extractDate(object: Any?) throws -> Date {
      guard let rawDate = object as? String else {
        throw MapperError.convertibleError(value: object, type: Date.self)
      }
        let dateFormatter = NSDateFormatter()
        dateFormatter.dateFormat = "your date format"
    
        if let date = dateFormatter.date(from: rawDate)  {
            return date
        } else {
          throw MapperError.convertibleError(value: object, type: Date.self)
        }
    
    }
    
    struct DateModel: Mappable {
      let date: Date
    
      init(map: Mapper) throws {
        try date = map.from("date", transformation: extractDate)
      }
    }