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

# init(fileURL:options:documentAttributes:)

Initializes a new attributed string object from the data at the specified URL.

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

## Parameters

`url`

An `NSURL` object specifying the document to load.

`options`

Document attributes for interpreting the document contents. [`documentType`](/documentation/Foundation/NSAttributedString/DocumentAttributeKey/documentType), [`characterEncoding`](/documentation/Foundation/NSAttributedString/DocumentAttributeKey/characterEncoding), and [`defaultAttributes`](/documentation/Foundation/NSAttributedString/DocumentAttributeKey/defaultAttributes) are supported option keys. If not specified, the method examines the data to attempt to determine the appropriate attributes.

`dict`

If non-`NULL`, returns a dictionary with various document-wide attributes accessible via document attribute keys.

## Return Value

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

## Discussion

The HTML importer should not be called from a background thread (that is, the `options` dictionary includes [`documentType`](/documentation/Foundation/NSAttributedString/DocumentAttributeKey/documentType) with a value of [`html`](/documentation/Foundation/NSAttributedString/DocumentType/html)). It will try to synchronize with the main thread, fail, and time out. Calling it from the main thread works (but can still time out if the HTML contains references to external resources, which should be avoided at all costs). The HTML import mechanism is meant for implementing something like markdown (that is, text styles, colors, and so on), not for general HTML import.

> 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)