<!--
{
  "availability" : [
    "iOS: 9.0.0 -",
    "iPadOS: 9.0.0 -",
    "macCatalyst: 13.1.0 -",
    "macOS: 10.4.0 -",
    "swift: -",
    "tvOS: 9.0.0 -",
    "visionOS: 1.0.0 -",
    "watchOS: 2.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "Foundation",
  "identifier" : "/documentation/Foundation/NSAttributedString/init(URL:options:documentAttributes:)",
  "metadataVersion" : "0.1.0",
  "role" : "Initializer",
  "symbol" : {
    "kind" : "Initializer",
    "modules" : [
      "Foundation"
    ],
    "preciseIdentifier" : "c:objc(cs)NSAttributedString(im)initWithURL:options:documentAttributes:error:"
  },
  "title" : "init(URL:options:documentAttributes:)"
}
-->

# init(URL:options:documentAttributes:)

Creates an attributed string from the contents of the specified URL.

```
init(URL url: URL, options: [NSAttributedString.DocumentReadingOptionKey : Any] = [:], documentAttributes dict: AutoreleasingUnsafeMutablePointer<NSDictionary?>?) throws
```

## Parameters

`url`

An `NSURL` object specifying the document to load.

`options`

Attributes for interpreting the document contents. Specify the [`documentType`](/documentation/Foundation/NSAttributedString/DocumentAttributeKey/documentType) or [`fileType`](/documentation/Foundation/NSAttributedString/DocumentReadingOptionKey/fileType) option to interpret the data as a specific type. When sharing files between different platforms, specify the [`sourceTextScaling`](/documentation/Foundation/NSAttributedString/DocumentReadingOptionKey/sourceTextScaling) or [`targetTextScaling`](/documentation/Foundation/NSAttributedString/DocumentReadingOptionKey/targetTextScaling) options for any required text scaling behaviors. Specify the [`characterEncoding`](/documentation/Foundation/NSAttributedString/DocumentAttributeKey/characterEncoding) attribute for plain-text files. Specify the [`defaultAttributes`](/documentation/Foundation/NSAttributedString/DocumentAttributeKey/defaultAttributes) key to apply document attributes to the returned string. If you specify an empty dictionary, the method identifies the data format from the data itself.

`dict`

An in-out dictionary containing document-level attributes. On output, this method updates the dictionary to contain any document-specific keys found in the data. Specify `nil` if you don’t want the document attributes.

## Return Value

Returns an initialized attributed string object, or `nil` if the method can’t decode the data.

## Discussion

Filter services can be used to convert the file into a format recognized by Cocoa. The `options` dictionary specifies how the document should be loaded and can contain the values described in <doc://com.apple.documentation/documentation/UIKit/NSAttributedStringDocumentReadingOptionKey>. If you specify the [`documentType`](/documentation/Foundation/NSAttributedString/DocumentReadingOptionKey/documentType) or [`fileType`](/documentation/Foundation/NSAttributedString/DocumentReadingOptionKey/fileType) attribute, this method treats the data as if it is in the specified format. If you don’t specify one of these options, the method examines the document and loads it using whatever format it seems to contain.

If an error occurs, the method returns `nil` and sets the `error` parameter to an [`NSError`](/documentation/Foundation/NSError) object with information about why it couldn’t create the attributed string object.

> Handling Errors in Swift:
> In Swift, this API is imported as an initializer 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)