<!--
{
  "availability" : [
    "iOS: 2.0.0 -",
    "iPadOS: 2.0.0 -",
    "macCatalyst: 13.0.0 -",
    "macOS: 10.0.0 -",
    "tvOS: 9.0.0 -",
    "visionOS: 1.0.0 -",
    "watchOS: 2.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "Foundation",
  "identifier" : "/documentation/Foundation/Formatter/string(for:)",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "Foundation"
    ],
    "preciseIdentifier" : "c:objc(cs)NSFormatter(im)stringForObjectValue:"
  },
  "title" : "string(for:)"
}
-->

# string(for:)

The default implementation of this method raises an exception.

```
func string(for obj: Any?) -> String?
```

## Parameters

`obj`

The object for which a textual representation is returned.

## Return Value

An `NSString` object that textually represents `object` for display. Returns `nil` if `object` is not of the correct class.

## Discussion

When implementing a subclass, return the `NSString` object that textually represents the cell’s object for display and—if [`editingString(for:)`](/documentation/Foundation/Formatter/editingString(for:)) is unimplemented—for editing. First test the passed-in object to see if it’s of the correct class. If it isn’t, return `nil`; but if it is of the right class, return a properly formatted and, if necessary, localized string. (See the specification of the [`NSString`](/documentation/Foundation/NSString) class for formatting and localizing details.)

The following implementation (which is paired with the [`getObjectValue(_:for:errorDescription:)`](/documentation/Foundation/Formatter/getObjectValue(_:for:errorDescription:)) example above) prefixes a two-digit float representation with a dollar sign:

```objc
- (NSString *)stringForObjectValue:(id)anObject {
 
    if (![anObject isKindOfClass:[NSNumber class]]) {
        return nil;
    }
    return [NSString stringWithFormat:@"$%.2f", [anObject  floatValue]];
}
```

## See Also

  [Data Formatting Guide](https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/DataFormatting/DataFormatting.html#//apple_ref/doc/uid/10000029i)

[`getObjectValue(_:for:errorDescription:)`](/documentation/Foundation/Formatter/getObjectValue(_:for:errorDescription:))

The default implementation of this method raises an exception.



---

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)