nsdata - Is there a writeToFile equivalent for Swift 3's 'Data' type? -


i've code returns new ios 10 / swift 3 nsdata replacement(?) type: data

if let jpegdata = uiimagejpegrepresentation(newimage, 0.8) { ... } 

i want write image disk, nsdata's writetofile: method not present class. have writetourl: method, doesn't seem work file path (and appending component).

can clarify how this, used case in swift 2:

jpegdata.writetofile(imagepath, atomically: true)  

thanks!

use write(to: fileurl).

for example:

let fileurl = try! filemanager.default.url(for: .documentdirectory, in: .userdomainmask, appropriatefor: nil, create: false).appendingpathcomponent("test.jpg")  {     try jpegdata.write(to: fileurl, options: .atomic) } catch {     print(error) } 

or, if stuck path, convert url:

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

but, generally, it's preferable use url references throughout code nowadays, retiring use of path strings.


Comments