A closure and protocol based framework for ValueTransformer
, helpful functions to register ValueTransformer
by identifier.
let transformer = ValueTransformer.closure { object in
return ...
}
A ValueTransformer
subclass is no more necessary using this method.
Implement ValueTransformerType
or ResersableValueTransformerType
and an a computed property transformer
will be accessible.
Define your enum
, a list of all your enum case in transformers
, implement the function transformedValue
of ValueTransformerType
protocol
enum StringTransformers: String, ValueTransformers, ValueTransformerType {
case capitalized, lowercased, uppercased
public static let transformers: [StringTransformers] = [.capitalized, .lowercased, .uppercased]
public func transformedValue(_ value: Any?) -> Any? { ../* string manipulation */ }
}
You can retrieve a value transformer using optional initializer of ValueTransformer
: init?(forName: NSValueTransformerName)
A protocol ValueTransformerRegisterable
help you to register your ValueTransformer
By providing a value transformer and an identifier name
, a new method will be available
myValueTransformer.register()
So just defined an identifier name
in your ValueTransformerType
struct MyTransformer: ValueTransformerType, ValueTransformerRegisterable {
var name = NSValueTransformerName(rawValue: "MyTransformation")
You can define a singleton instance using ValueTransformerSingleton
struct MyTransformer: ValueTransformerType, ValueTransformerRegisterable, ValueTransformerSingleton {
var name = NSValueTransformerName(rawValue: "MyTransformation")
public static let instance = MyTransformer()
or a static function will help you to register it
MyTransformer.register() // same as MyTransformer.instance.register()
enum StringTransformers: String, ValueTransformers, ValueTransformerType {
...
var name: NSValueTransformerName {
return NSValueTransformerName("String" + self.rawValue.capitalized)
}
then you can register one by one
StringTransformers.capitalized.register()
or all case
StringTransformers.register()
- capitalized
- uppercased
- lowercased
- PNG Representation
- JPEG Representation
- RFC 2822 Timestamp*
- Using
DateFormatter.Style
- Using
NumberFormatter.Style
- Using
NSLocale.Key
- Using
IsEmpty
(resp.IsNotEmpty
) you could transformString
,NSString
,Array
,NSArray
,Dictionnary
, etc... to boolean.true
(resp.false
) if empty
ValueTransformerKit is available under the MIT license. See the LICENSE file for more info.