---
title: HKAttachment
framework: healthkit
role: symbol
role_heading: Class
path: healthkit/hkattachment
---

# HKAttachment

A file that is attached to a sample in the HealthKit store.

## Declaration

```swift
class HKAttachment
```

## Overview

Overview To access the attachment’s data, get a data reader from the attachment store for each attachment. let attachmentStore = HKAttachmentStore(healthStore: store)

let attachments: [HKAttachment] do {     attachments = try await attachmentStore.attachments(for: prescription) } catch {     // Handle the error here.     fatalError("*** An error occurred while accessing the attachments for a prescription: \(error.localizedDescription) ***") }

// Use the attachments here. print("*** \(attachments.count) attachments found ***")

for attachment in attachments {

// Get a data reader for the attachment.     let dataReader = attachmentStore.dataReader(for:   attachment)

// Read the data here. } You can then asynchronously access the whole data object. let data: Data do {     data = try await dataReader.data } catch {     // Handle the error here.     fatalError("*** An error occurred while accessing the attachment's data. \(error.localizedDescription) ***") } Alternatively, you can access the file’s contents as an asynchronous sequence of bytes. // Asynchronously access the attachment's bytes. var data = Data() do {     for try await byte in dataReader.bytes {         // Use the bytes here.         data.append(byte)     } } catch {     // Handle the error here.     fatalError("*** An error occurred while reading the attachment's data: \(error.localizedDescription) ***") } note: You can only add attachments to HKVisionPrescription, HKGlassesPrescription, and HKContactsPrescription samples. You can also read attachments from clinicalNoteRecord samples.

## Topics

### Accessing attachment data

- [name](healthkit/hkattachment/name.md)
- [identifier](healthkit/hkattachment/identifier.md)
- [contentType](healthkit/hkattachment/contenttype.md)
- [size](healthkit/hkattachment/size.md)
- [creationDate](healthkit/hkattachment/creationdate.md)
- [metadata](healthkit/hkattachment/metadata.md)
- [HKAttachment.AsyncBytes](healthkit/hkattachment/asyncbytes.md)

### Initializers

- [init(coder:)](healthkit/hkattachment/init(coder:).md)

## Relationships

### Inherits From

- [NSObject](objectivec/nsobject-swift.class.md)

### Conforms To

- [CVarArg](swift/cvararg.md)
- [CustomDebugStringConvertible](swift/customdebugstringconvertible.md)
- [CustomStringConvertible](swift/customstringconvertible.md)
- [Equatable](swift/equatable.md)
- [Hashable](swift/hashable.md)
- [NSCoding](foundation/nscoding.md)
- [NSCopying](foundation/nscopying.md)
- [NSObjectProtocol](objectivec/nsobjectprotocol.md)
- [NSSecureCoding](foundation/nssecurecoding.md)
- [Sendable](swift/sendable.md)
- [SendableMetatype](swift/sendablemetatype.md)

## See Also

### Related Documentation

- [HKVisionPrescription](healthkit/hkvisionprescription.md)
- [HKGlassesPrescription](healthkit/hkglassesprescription.md)
- [HKContactsPrescription](healthkit/hkcontactsprescription.md)

### Attachments

- [HKAttachmentStore](healthkit/hkattachmentstore.md)
- [HKAttachmentDataReader](healthkit/hkattachmentdatareader.md)
