Contents

ValueTransformer

An abstract class used to transform values from one representation to another.

Declaration

class ValueTransformer

Overview

You create a value transformer by subclassing ValueTransformer and overriding the necessary methods to provide the required custom transformation. You then register the value transformer using the setValueTransformer(_:forName:) method, so that other parts of your app can access it by name with init(forName:).

Use the transformedValue(_:) method to transform a value from one representation into another. If a value transformer designates that its transformation is reversible by returning true for allowsReverseTransformation(), you can also use the reverseTransformedValue(_:) to perform the transformation in reverse. For example, reversing the characters in a string is a reversible operation, whereas changing the characters in a string to be uppercase is a nonreversible operation.

A value transformer can take inputs of one type and return a value of a different type. For example, a value transformer could take an NSImage or UIImage object and return an NSData object containing the PNG representation of that image.

Example Usage

The following example defines a new value transformer that takes an object and returns a string based on the object’s class type. This transformer isn’t reversible because it doesn’t make sense to transform a class name into an object.

Topics

Using the Name-Based Registry

Getting Information About a Transformer

Transforming Values

See Also

Value Wrappers and Transformations