我有一个类,它正被映射到ObjectMapper。在过去,映射工作得很好,但是项目体系结构的改变使得webservice返回的字段Double变成了带有加密值的String
在映射过程中,是否可以对webservice字段进行解密并将其转换为my class变量?
这就是我今天所拥有的:

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

最佳答案

有几种方法可以完成数据解密。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"]
    }

}

关于ios - 在ObjectMapper中使用加密,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/46042797/

10-12 05:05