我有一个用ObjectMapper
映射的类。在过去,映射工作正常,但项目体系结构的更改使Web服务返回Double
字段为String
并具有密码值。
是否有可能在映射期间解密并将webservice字段转换为我的类变量?
这就是我今天所拥有的:
class PosicaoConsolidada: Mappable {
var portifolioBalance: Double!
var families: [Family]!
required init?(map: Map) {}
public func mapping(map: Map) {
portifolioBalance <- map["pointsBalance"]
families <- map["fam"]
}
}
这是我想象的事情:
class PosicaoConsolidada: Mappable {
var portifolioBalance: Double!
var families: [Family]!
required init?(map: Map) {}
public func mapping(map: Map) {
portifolioBalance <- Double(Decryptor.decrypt(map["pointsBalance"]))
families <- map["fam"]
}
}
答案 0 :(得分:1)
有几种方法可以完成数据解密。 ObjectMapper提供协议TransformType
以在映射(reference)时处理数据转换。
变压器:
import ObjectMapper
class PointsTransform: TransformType {
typealias Object = Double
typealias JSON = String
func transformFromJSON(_ value: Any?) -> Object? {
guard let encoded = value as? String
else { return nil }
// TODO: Replace with decoding logic
return 239_584.938
}
func transformToJSON(_ value: Object?) -> JSON? {
guard let decoded = value
else { return nil }
return "\(decoded)"
}
}
型号:
class PosicaoConsolidada: Mappable {
var portifolioBalance: Double!
var families: [Family]!
required init?(map: Map) {}
public func mapping(map: Map) {
portifolioBalance <- (map["pointsBalance"], PointsTransform())
families <- map["fam"]
}
}