Skip to content

Instantly share code, notes, and snippets.

@tomduckering
Forked from gonzalezreal/ValueTransformer.swift
Last active October 25, 2018 23:49
Show Gist options
  • Save tomduckering/6976e984f575249b7f66320ac1d978f2 to your computer and use it in GitHub Desktop.
Save tomduckering/6976e984f575249b7f66320ac1d978f2 to your computer and use it in GitHub Desktop.
Type safe NSValueTransformer
func stringToInt(value: String) -> Int? {
return value.toInt()
}
func intToString(value: Int) -> String? {
return "\(value)"
}
NSValueTransformer.setValueTransformerWithName("StringToInt", transform: stringToInt, reverseTransform: intToString)
import Foundation
private class BasicValueTransformer: ValueTransformer {
let transform: (AnyObject?) -> (AnyObject?)
init(transform: @escaping (AnyObject?) -> (AnyObject?)) {
self.transform = transform
}
// MARK: NSValueTransformer
override class func allowsReverseTransformation() -> Bool {
return false
}
override class func transformedValueClass() -> AnyClass {
return NSObject.self
}
func transformedValue(value: AnyObject?) -> AnyObject? {
return transform(value)
}
}
private class ReversibleValueTransformer: BasicValueTransformer {
let reverseTransform: (AnyObject?) -> (AnyObject?)
init(transform: @escaping (AnyObject?) -> (AnyObject?), reverseTransform: @escaping (AnyObject?) -> (AnyObject?)) {
self.reverseTransform = reverseTransform
super.init(transform: transform)
}
// MARK: NSValueTransformer
override class func allowsReverseTransformation() -> Bool {
return true
}
func reverseTransformedValue(value: AnyObject?) -> AnyObject? {
return reverseTransform(value)
}
}
public extension ValueTransformer {
/**
Registers a value transformer with a given name and transform function.
:param: name The name of the transformer.
:param: transform The function that performs the transformation.
*/
class func setValueTransformerWithName<T, U>(name: String, transform: @escaping (T) -> (U?)) {
let transformer = BasicValueTransformer { value in
return (value as? T).flatMap {
transform($0) as? AnyObject
}
}
self.setValueTransformer(transformer, forName: NSValueTransformerName(name))
}
/**
Registers a reversible value transformer with a given name and transform functions.
:param: name The name of the transformer.
:param: transform The function that performs the forward transformation.
:param: reverseTransform The function that performs the reverse transformation.
*/
class func setValueTransformerWithName<T, U>(name: String, transform: @escaping (T) -> (U?), reverseTransform: @escaping (U) -> (T?)) {
let transformer = ReversibleValueTransformer(transform: { value in
return (value as? T).flatMap {
transform($0) as? AnyObject
}
}, reverseTransform: { value in
return (value as? U).flatMap {
reverseTransform($0) as? AnyObject
}
})
self.setValueTransformer(transformer, forName: NSValueTransformerName(name))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment