Class

NSValueTransformer

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

Declaration

@interface NSValueTransformer : NSObject

Overview

You create a value transformer by subclassing NSValueTransformer 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 valueTransformerForName:.

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 YES 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.

@interface ClassNameTransformer: NSValueTransformer {}
@end
@implementation ClassNameTransformer
+ (Class)transformedValueClass { 
    return [NSString class]; 
}
+ (BOOL)allowsReverseTransformation { 
    return NO; 
}
- (id)transformedValue:(id)value {
    return (value == nil) ? nil : NSStringFromClass([value class]);
}
@end

Topics

Using the Name-Based Registry

+ setValueTransformer:forName:

Registers the provided value transformer with a given identifier.

+ valueTransformerForName:

Returns the value transformer identified by a given identifier.

+ valueTransformerNames

Returns an array of all the registered value transformers.

NSValueTransformerName

Named value transformers defined by NSValueTransformer.

Getting Information About a Transformer

+ allowsReverseTransformation

Returns a Boolean value that indicates whether the receiver can reverse a transformation.

+ transformedValueClass

Returns the class of the value returned by the receiver for a forward transformation.

Transforming Values

- transformedValue:

Returns the result of transforming a given value.

- reverseTransformedValue:

Returns the result of the reverse transformation of a given value.

Relationships

Inherits From

See Also

Value Wrappers and Transformations

NSNumber

An object wrapper for primitive scalar numeric values.

NSValue

A simple container for a single C or Objective-C data item.

Beta Software

This documentation contains preliminary information about an API or technology in development. This information is subject to change, and software implemented according to this documentation should be tested with final operating system software.

Learn more about using Apple's beta software