swift - FileHandle not accepting my URLs for write access -


i'd open uniquely named output file writing either plist or data, not having luck in getting handle using either url routine of init(fileurlwithpath:) or init(string:)

func newfilehandleforwritingfile(path: string, name: string, type: string, outfile: inout string?) -> filehandle? {     let fm = filemanager.default     var file: string? = nil     var uniquenum = 0      while true {         let tag = (uniquenum > 0 ? string(format: "-%d", uniquenum) : "")         let unique = string(format: "%@%@.%@", name, tag, type)         file = string(format: "%@/%@", path, unique)         if false == fm.fileexists(atpath: file!) { break }          // try tag.         uniquenum += 1;     }      outfile = file!      {          let fileurl = url.init(fileurlwithpath: file!)         let filehandle = try filehandle.init(forwritingto: fileurl)         print("\(file!) opened writing")         //set file extension hidden attribute yes         try fm.setattributes([fileattributekey.extensionhidden: true], ofitematpath: file!)         return filehandle     } catch let error {         nsapp.presenterror(error)         return nil;     } } 

debugger shows

debugger shows

which url init routine adds scheme (file://) otherwise same other, , i'd prefer newer methods throw reutrning (-1) when using paths. error thrown (2) enoent (no such entity!?) need handle write to i'm confused how else one? sample path new folder created @ desktop triage.

you can't create file handle non-existent file. causing enoent error.

use filemanager createfile(atpath:contents:attributes:) create file before creating file handle.

do {     fm.createfile(atpath: file!, contents: nil, attributes: [fileattributekey.extensionhidden: true])     let fileurl = url(fileurlwithpath: file!)     let filehandle = try filehandle(forwritingto: fileurl)     print("\(file!) opened writing")      return filehandle } catch let error {     nsapp.presenterror(error)     return nil; } 

Comments

Popular posts from this blog

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

python Tkinter Capturing keyboard events save as one single string -

sql server - Why does Linq-to-SQL add unnecessary COUNT()? -