Closed nbabaka closed 4 years ago
{ "items": [{ "id": 74969, "product_id": 396, "content_id": 2412, "created_at": "2019-07-24 20:17:31", }] }
class Purchase: Mappable { var id: Int! var contentId: Int! // content_id var productId: Int! // product_id var expired: Date! required init?(map: Map) {} func mapping(map: Map) { let transform = TransformOf<Date, String>(fromJSON: { (value: String?) -> Date? in let dateFormatter = DateFormatter() dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss" dateFormatter.timeZone = TimeZone(identifier: "UTC") let date = dateFormatter.date(from: value ?? "") return date }, toJSON: { (value: Date?) -> String? in if let date = value { let dateFormatter = DateFormatter() dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss" return dateFormatter.string(from: date) } return nil }) expired <- (map["real_expired_at"], transform) } }
let repo = Mapper<Purchase>().mapArray(JSONString: json["items"])
I expected something like:
Purchase(date: "some date object")
Purchase(date: nil)
All other variables without custom transform are good.
oh (( XCODE debugger gave me wrong data (((
Your JSON dictionary:
Your model:
What you did:
What you expected:
I expected something like:
What you got:
All other variables without custom transform are good.