Objectmapper: cómo mapear [Int64]?

Creado en 11 ago. 2016  ·  4Comentarios  ·  Fuente: tristanhimmelman/ObjectMapper

el código no funciona, cómo manejar [Int64] o 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
        }
  }

Comentario más útil

Ampliando la respuesta de @josejuanqm , aquí hay una clase que se puede reutilizar si las instancias de clase global no son lo tuyo:

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
    }
}

Todos 4 comentarios

Podrías usar esta transformación ->

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
})

y modifíquelo para transformarlo en una matriz.

entonces úsalo así ->

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

¡Espero eso ayude!

¡¡Muchas gracias!!

Ampliando la respuesta de @josejuanqm , aquí hay una clase que se puede reutilizar si las instancias de clase global no son lo tuyo:

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
    }
}

Los campos int64 obtuvieron un valor nulo en ObjectMapper 2.2.5 con la solución anterior. Analicé el valor de Int64 directamente desde un objeto String.

Fundación de importación
importar ObjectMapper

clase Int64Transformer: TransformType {
alias de tipo público Objeto = Int64
alias de tipo público 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
}

}

¿Fue útil esta página
0 / 5 - 0 calificaciones

Temas relacionados

nearspears picture nearspears  ·  4Comentarios

danyalaytekin picture danyalaytekin  ·  4Comentarios

adasoft-dev picture adasoft-dev  ·  3Comentarios

amg1976 picture amg1976  ·  3Comentarios

mirzadelic picture mirzadelic  ·  4Comentarios