<!--
{
  "availability" : [
    "iOS: 7.0.0 -",
    "iPadOS: 7.0.0 -",
    "macCatalyst: 13.1.0 -",
    "macOS: 10.0.0 -",
    "tvOS: 9.0.0 -",
    "visionOS: 1.0.0 -",
    "watchOS: 2.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "Foundation",
  "identifier" : "/documentation/Foundation/NSMutableAttributedString/read(from:options:documentAttributes:)-5mbcx",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "Foundation"
    ],
    "preciseIdentifier" : "c:objc(cs)NSMutableAttributedString(im)readFromData:options:documentAttributes:error:"
  },
  "title" : "read(from:options:documentAttributes:)"
}
-->

# read(from:options:documentAttributes:)

Sets the contents of the attributed string using the specified data object`.`

```
func read(from data: Data, options opts: [NSAttributedString.DocumentReadingOptionKey : Any] = [:], documentAttributes dict: AutoreleasingUnsafeMutablePointer<NSDictionary?>?, error: ()) throws
```

## Parameters

`data`

The data object providing text data.

`opts`

Keys specifying the types of documents and other document import options. For a list of possible values, see “Option keys for importing documents” in [`NSAttributedString`](/documentation/Foundation/NSAttributedString)

`dict`

On return, the dictionary (if provided) contains keys representing various document-wide attributes. For a list of possible values, see “Document Attributes” in [`NSAttributedString`](/documentation/Foundation/NSAttributedString).

`error`

Upon return, if an error occurs, contains an `NSError` object that describes the problem. If you are not interested in possible errors, pass in `NULL`.

## Discussion

`opts` can contain one of the values described in the Constants section of NSAttributedString Application Kit Additions Reference (“Option keys for importing documents”).

On return, the `documentAttributes` dictionary (if provided) contains the various keys described in the Constants section of NSAttributedString Application Kit Additions Reference. If unsuccessful, returns NO , after setting `error` to point to an `NSError` object that encapsulates the reason why the attributed string object could not be created.

> Handling Errors in Swift:
> In Swift, this method returns `Void` 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](https://docs.swift.org/swift-book/LanguageGuide/ErrorHandling.html) in [The Swift Programming Language](https://docs.swift.org/swift-book/) and `About Imported Cocoa Error Parameters`.

---

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)