---
title: "string(for:)"
framework: foundation
role: symbol
role_heading: Instance Method
path: "foundation/formatter/string(for:)"
---

# string(for:)

The default implementation of this method raises an exception.

## Declaration

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

## Parameters

- `obj`: The object for which a textual representation is returned.

## Return Value

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

## Discussion

Discussion When implementing a subclass, return the NSString object that textually represents the cell’s object for display and—if 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 class for formatting and localizing details.) The following implementation (which is paired with the getObjectValue(_:for:errorDescription:) example above) prefixes a two-digit float representation with a dollar sign: - (NSString *)stringForObjectValue:(id)anObject {       if (![anObject isKindOfClass:[NSNumber class]]) {         return nil;     }     return [NSString stringWithFormat:@"$%.2f", [anObject  floatValue]]; }

## See Also

### Related Documentation

- [Data Formatting Guide](apple-archive/documentation/Cocoa/Conceptual/DataFormatting.md)
- [getObjectValue(_:for:errorDescription:)](foundation/formatter/getobjectvalue(_:for:errordescription:).md)

### Getting Textual Representations of Object Values

- [attributedString(for:withDefaultAttributes:)](foundation/formatter/attributedstring(for:withdefaultattributes:).md)
- [editingString(for:)](foundation/formatter/editingstring(for:).md)
