<!--
{
  "availability" : [
    "iOS: 3.0.0 -",
    "iPadOS: 3.0.0 -",
    "macCatalyst: 13.1.0 -",
    "macOS: 10.3.0 -",
    "tvOS: 9.0.0 -",
    "visionOS: 1.0.0 -",
    "watchOS: 2.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "Foundation",
  "identifier" : "/documentation/Foundation/ValueTransformer",
  "metadataVersion" : "0.1.0",
  "role" : "Class",
  "symbol" : {
    "kind" : "Class",
    "modules" : [
      "Foundation"
    ],
    "preciseIdentifier" : "c:objc(cs)NSValueTransformer"
  },
  "title" : "ValueTransformer"
}
-->

# ValueTransformer

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

```
class ValueTransformer
```

## Overview

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

Use the [`transformedValue(_:)`](/documentation/Foundation/ValueTransformer/transformedValue(_:)) method to transform a value from one representation into another. If a value transformer designates that its transformation is reversible by returning <doc://com.apple.documentation/documentation/Swift/true> for [`allowsReverseTransformation()`](/documentation/Foundation/ValueTransformer/allowsReverseTransformation()), you can also use the [`reverseTransformedValue(_:)`](/documentation/Foundation/ValueTransformer/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 <doc://com.apple.documentation/documentation/AppKit/NSImage> or <doc://com.apple.documentation/documentation/UIKit/UIImage> object and return an [`NSData`](/documentation/Foundation/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.

```swift
class ClassNameTransformer: ValueTransformer {
    override class func transformedValueClass() -> AnyClass {
        return NSString.self
    }
    
    override class func allowsReverseTransformation() -> Bool {
        return false
    }
    
    override func transformedValue(_ value: Any?) -> Any? {
        return (value as AnyObject).className
    }
}

extension NSValueTransformerName {
    static let classNameTransformerName = NSValueTransformerName(rawValue: "ClassNameTransformer")
}

ValueTransformer.setValueTransformer(ClassNameTransformer(), forName: .classNameTransformerName)
```

## Topics

### Using the Name-Based Registry

[`+  setValueTransformer:forName:`](/documentation/Foundation/ValueTransformer/setValueTransformer(_:forName:))

Registers the provided value transformer with a given identifier.

[`+  valueTransformerForName:`](/documentation/Foundation/ValueTransformer/init(forName:))

Returns the value transformer identified by a given identifier.

[`+  valueTransformerNames`](/documentation/Foundation/ValueTransformer/valueTransformerNames())

Returns an array of all the registered value transformers.

[`NSValueTransformerName`](/documentation/Foundation/NSValueTransformerName)

Named value transformers defined by `NSValueTransformer`.

### Getting Information About a Transformer

[`+  allowsReverseTransformation`](/documentation/Foundation/ValueTransformer/allowsReverseTransformation())

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

[`+  transformedValueClass`](/documentation/Foundation/ValueTransformer/transformedValueClass())

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

### Transforming Values

[`-  transformedValue:`](/documentation/Foundation/ValueTransformer/transformedValue(_:))

Returns the result of transforming a given value.

[`-  reverseTransformedValue:`](/documentation/Foundation/ValueTransformer/reverseTransformedValue(_:))

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

## Relationships

### Inherits From

[`NSObject-swift.class`](/documentation/ObjectiveC/NSObject-swift.class)

### Conforms To

[`Equatable`](/documentation/Swift/Equatable)

[`Hashable`](/documentation/Swift/Hashable)

[`NSObjectProtocol`](/documentation/ObjectiveC/NSObjectProtocol)

[`CVarArg`](/documentation/Swift/CVarArg)

[`CustomStringConvertible`](/documentation/Swift/CustomStringConvertible)

[`CustomDebugStringConvertible`](/documentation/Swift/CustomDebugStringConvertible)

### Inherited By

[`NSSecureUnarchiveFromDataTransformer`](/documentation/Foundation/NSSecureUnarchiveFromDataTransformer)

---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)