---
title: "dataReader(for:)"
framework: healthkit
role: symbol
role_heading: Instance Method
path: "healthkit/hkattachmentstore/datareader(for:)"
---

# dataReader(for:)

Returns a data reader for the attachment.

## Declaration

```swift
func dataReader(for attachment: HKAttachment) -> HKAttachmentDataReader
```

## Parameters

- `attachment`: An attachment associated with an object in the HealthKit store.

## Discussion

Discussion Call this method to access a data reader for the attachment’s contents. // Get a data reader for the attachment. let dataReader = attachmentStore.dataReader(for: myAttachment) You can then read the attachment’s contents from the data reader. 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) ***") }

// Use the data here. 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) ***") }

## See Also

### Accessing attachments

- [getAttachments(for:completion:)](healthkit/hkattachmentstore/getattachments(for:completion:).md)
- [getData(for:completion:)](healthkit/hkattachmentstore/getdata(for:completion:).md)
- [streamData(for:dataHandler:)](healthkit/hkattachmentstore/streamdata(for:datahandler:).md)
