Objectmapper: how to map [Int64]?

Created on 11 Aug 2016  路  4Comments  路  Source: tristanhimmelman/ObjectMapper

the code not working, how to handle [Int64] or Transform

 public struct TestObject Mappable {
        var check_time : [Int64]?    // not working
        public init?(_ map: Map) {
        }
        mutating public func mapping(map: Map) {
            check_time    <- map["check_time"] // not working
        }
  }

Most helpful comment

Expanding on the answer by @josejuanqm , here is a class that can be reused if global class instances aren't your bag:

import ObjectMapper

public class Int64Transform: TransformType {
    public typealias Object = Int64
    public typealias JSON = Int

    public func transformFromJSON(value: AnyObject?) -> Int64? {
        guard let value = value as? Int else {
            return nil
        }
        return Int64(value)
    }

    public func transformToJSON(value: Int64?) -> Int? {
        if let data = value {
            return Int(data)
        }
        return nil
    }
}

All 4 comments

You could use this transformation ->

public let transformInt64 = TransformOf<Int64, Int>(fromJSON: { (value: Int?) -> Int64? in
    guard let v = value else{
        return nil
    }
    return Int64(v)
    }, toJSON: { (value: Int64?) -> Int? in
        // transform value from Int? to String?
        if let value = value {
            return Int(value)
        }
        return nil
})

and modify it to transform into an array.

then use it like this ->

someVariable <- (map["someIntArray"], transformInt64)

Hope it helps!

Great!, thanks!!

Expanding on the answer by @josejuanqm , here is a class that can be reused if global class instances aren't your bag:

import ObjectMapper

public class Int64Transform: TransformType {
    public typealias Object = Int64
    public typealias JSON = Int

    public func transformFromJSON(value: AnyObject?) -> Int64? {
        guard let value = value as? Int else {
            return nil
        }
        return Int64(value)
    }

    public func transformToJSON(value: Int64?) -> Int? {
        if let data = value {
            return Int(data)
        }
        return nil
    }
}

The int64 fields got nil value in ObjectMapper 2.2.5 with the above solution. I parsed Int64 value directly from a String object.

import Foundation
import ObjectMapper

class Int64Transformer : TransformType {
public typealias Object = Int64
public typealias JSON = String

public func transformFromJSON(_ value: Any?) -> Int64? {
    guard let value = value as? String else {
        return nil
    }
    return Int64(value)
}

public func transformToJSON(_ value: Int64?) -> String? {
    if let data = value {
        return String(data)
    }
    return nil
}

}

Was this page helpful?
0 / 5 - 0 ratings

Related issues

maksTheAwesome picture maksTheAwesome  路  4Comments

AashishSapkota picture AashishSapkota  路  3Comments

quetool picture quetool  路  3Comments

liltimtim picture liltimtim  路  3Comments

danyalaytekin picture danyalaytekin  路  4Comments