Open ghost opened 6 years ago
the thing is, api response is
{
"operation" = " S "
}
and i want to trim before transform to enum
@TotaX you can achieve this by creating you own custom TransformType
like:
open class TrimmingStringEnumTransform<T: RawRepresentable>: TransformType where T.RawValue == String {
public typealias Object = T
public typealias JSON = String
private let charactersToTrimm: CharacterSet
public init(charactersToTrimm: CharacterSet) {
self.charactersToTrimm = charactersToTrimm
}
open func transformFromJSON(_ value: Any?) -> Object? {
guard var raw = value as? String else {
return nil
}
raw = raw.trimmingCharacters(in: charactersToTrimm)
return T(rawValue: raw)
}
open func transformToJSON(_ value: T?) -> JSON? {
guard let obj = value else {
return nil
}
return obj.rawValue
}
}
and use it in mapping(map:)
function like:
operation <- (map["operation"], TrimmingStringEnumTransform(charactersToTrimm: .whitespaces))
yeah. i did that. but i wanted to know if there was something to combine two transform. anyway, thanks so much!
Hello, i'd like to know is there is something to combine two transform into one. like
then i want to combine them into one
let trimAndTransformToEnum = trimTransform >> toEnum
and then use it as
operation <- (map["operation"], trimAndTransformToEnum() )
oroperation <- (map["operation"], trimTransform >> toEnum)
so. does exist something like
>>
to combine two transforms ?thanks in advance F