---
title: "data(from:documentAttributes:)"
framework: foundation
role: symbol
role_heading: Instance Method
path: "foundation/nsattributedstring/data(from:documentattributes:)"
---

# data(from:documentAttributes:)

Returns a data object that contains a text stream corresponding to the characters and attributes within the specified range.

## Declaration

```swift
func data(from range: NSRange, documentAttributes dict: [NSAttributedString.DocumentAttributeKey : Any] = [:]) throws -> Data
```

## Parameters

- `range`: The range.
- `dict`: A required dictionary specifying the document attributes. The dictionary contains values from Document Types and must at least contain doc://com.apple.foundation/documentation/Foundation/NSAttributedString/DocumentAttributeKey/documentType.

## Return Value

Return Value Returns the data for the attributed string, or nil if failure. When nil, error encapsulates the error information.

## Discussion

Discussion Raises an rangeException if any part of range lies beyond the end of the receiver’s characters. note: In Swift, this method returns a nonoptional result and is marked with the throws keyword to indicate that it throws an error in cases of failure. You call this method in a try expression and handle any errors in the catch clauses of a do statement, as described in Error Handling in The Swift Programming Language and About Imported Cocoa Error Parameters.

## See Also

### Exporting the string as data

- [fileWrapper(from:documentAttributes:)](foundation/nsattributedstring/filewrapper(from:documentattributes:).md)
- [docFormat(from:documentAttributes:)](foundation/nsattributedstring/docformat(from:documentattributes:).md)
- [rtf(from:documentAttributes:)](foundation/nsattributedstring/rtf(from:documentattributes:).md)
- [rtfd(from:documentAttributes:)](foundation/nsattributedstring/rtfd(from:documentattributes:).md)
- [rtfdFileWrapper(from:documentAttributes:)](foundation/nsattributedstring/rtfdfilewrapper(from:documentattributes:).md)
