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
Post a Comment