<!--
{
  "availability" : [
    "macOS: 10.11.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "AppKit",
  "identifier" : "/documentation/AppKit/NSTextAttachment/init(data:ofType:)",
  "metadataVersion" : "0.1.0",
  "role" : "Initializer",
  "symbol" : {
    "kind" : "Initializer",
    "modules" : [
      "AppKit"
    ],
    "preciseIdentifier" : "c:objc(cs)NSTextAttachment(im)initWithData:ofType:"
  },
  "title" : "init(data:ofType:)"
}
-->

# init(data:ofType:)

Creates a text attachment object with the specified data.

```
init(data contentData: Data?, ofType uti: String?)
```

## Parameters

`contentData`

Data to use for the text attachment contents. Can be `nil`.

`uti`

A uniform type identifier specifying the data type of the attachment contents. Can be `nil`.

## Return Value

A new `NSTextAttachment` object.

## Discussion

This method is the designated initializer for the `NSTextAttachment` class on iOS.

When either `contentData` or `uti` is `nil`, TextKit considers the receiver to be an attachment without document contents. In this case, the `NSAttributedString` external file writing methods try to save the value of the [`image`](/documentation/AppKit/NSTextAttachment/image) property instead.

---

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)