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