0

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")
    }
}
rmaddy
  • 314,917
  • 42
  • 532
  • 579
Nikhila Mohan
  • 2,000
  • 2
  • 15
  • 19

2 Answers2

2

Created an extension for Date and its worked for me

extension Date:Convertible
{
    public static func fromMap(_ value: Any) throws -> Date {
        guard let rawDate = value as? String else {
            throw MapperError.convertibleError(value: value, type: Date.self)
        }
        let dateFormatter = DateFormatter()
        dateFormatter.dateFormat = "yyyy-MM-dd"

        if let date = dateFormatter.date(from: rawDate)  {
            return date
        } else {
            throw MapperError.convertibleError(value: value, type: Date.self)
        }


    }

}

Nikhila Mohan
  • 2,000
  • 2
  • 15
  • 19
1

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)
  }
}
Denys
  • 114
  • 1
  • 6