Is there a writeToFile equivalent for Swift 3’s ‘Data’ type?

Use write(to: fileURL).

For example:

let fileURL = try! FileManager.default
    .url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: false)
    .appendingPathComponent("test.jpg")

do {
    try jpegData.write(to: fileURL, options: .atomic)
} catch {
    print(error)
}

Or, if you really are stuck with a path, convert that to a URL:

do {
    try data.write(to: URL(fileURLWithPath: path), options: .atomic)
} catch {
    print(error)
}

But, generally, it’s preferable to use URL references throughout your code nowadays, retiring the use of path strings.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)