swift - Why can't I append a String to a NSURL? -


appending .txt file component url path doesn't work:

var error:nserror? let manager = nsfilemanager.defaultmanager() let docurl = manager.urlfordirectory(.documentdirectory, indomain:.userdomainmask, appropriateforurl:nil, create:true, error:&error) docurl.urlbyappendingpathcomponent("/ricfile.txt") <-- doesn't work 

via debugger:

file:///users/ric/library/developer/coresimulator/devices/ <device id>/data/containers/data/application/<app id>/documents/ 

writing string using docurl file doesn't work because of missing file name.

reason (via error):

"the operation couldn’t completed. directory"

so question: why doesn't following work?

docurl.urlbyappendingpathcomponent("/ricfile.txt") 

urlbyappendingpathcomponent: doesn't mutate existing nsurl, creates new one. documentation:

urlbyappendingpathcomponent: returns new url made appending path component original url.

you'll need assign return value of method something. example:

let directoryurl = manager.urlfordirectory(.documentdirectory, indomain:.userdomainmask, appropriateforurl:nil, create:true, error:&error) let docurl = directoryurl.urlbyappendingpathcomponent("/ricfile.txt") 

even better use nsurl(string:string, relativeto:nsurl):

let docurl = nsurl(string:"ricfile.txt", relativeto:directoryurl)  

Comments

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -