---
title: "init(contentsOf:)"
framework: foundation
role: symbol
role_heading: Initializer
path: "foundation/nsdata/init(contentsof:)"
---

# init(contentsOf:)

Creates a data object from the data at the specified file URL, or returns nil if the system can’t create one.

## Declaration

```swift
init?(contentsOf url: URL)
```

## Parameters

- `url`: The location on disk of the data to read.

## Discussion

Discussion important: As this method runs synchronously and blocks the calling thread until it finishes, don’t invoke it from the main thread. Use file coordination or one of the nonblocking file-related APIs instead. If you specify a malformed URL or the referenced location doesn’t exist on disk, the initializer fails and returns nil. To handle such errors, use NSData/init(contentsOfURL:options:)-5abi3 instead.
