<!--
{
  "availability" : [
    "iOS: -",
    "iPadOS: -",
    "visionOS: 1.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "RealityKit",
  "identifier" : "/documentation/RealityKit/Attachment/init(id:_:)",
  "metadataVersion" : "0.1.0",
  "role" : "Initializer",
  "symbol" : {
    "kind" : "Initializer",
    "modules" : [
      "RealityKit",
      "SwiftUI"
    ],
    "preciseIdentifier" : "s:19_RealityKit_SwiftUI10AttachmentV2id_ACyxGs11AnyHashableV_xyctcfc"
  },
  "title" : "init(id:_:)"
}
-->

# init(id:_:)

Creates an new attachment from an identifier and a closure.

```
nonisolated init(id: AnyHashable, @ViewBuilder _ content: @escaping () -> Content)
```

## Parameters

`id`

An `AnyHashable` instance that identifies the attachment and
a [`ViewAttachmentEntity`](/documentation/RealityKit/ViewAttachmentEntity).

`content`

A `ViewBuilder` instance that contains the views for the
attachment.

## Discussion

You can access details of an attachment entity, such as its bounds,
by calling the methods of a [`ViewAttachmentEntity`](/documentation/RealityKit/ViewAttachmentEntity) instance.
For example, you can add an attachment that contains the text `"hello"`.

```swift
Attachment(id: "example") {
    Text("hello")
}
```

> Note: The initializer doesn’t automatically add the views to a
> ``doc://com.apple.RealityKit/documentation/RealityKit/RealityView`` instance.
> You need to explicitly add each entity to your app’s scene hierarchy
> by directly placing an entity attachment to the view or as a child of
> another entity that’s already in the view.

---

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)