ios - How to access file included in app bundle in Swift? -
i know there few questions pertaining this, they're in objective-c.
how can access .txt
file included in app using swift on actual iphone? want able read , write it. here project files if want take look. i'm happy add details if necessary.
simply searching in app bundle resource
var filepath = nsbundle.mainbundle().urlforresource("file", withextension: "txt")
however can't write because in app resources directory , have create in document directory write it
var documentsdirectory: nsurl? var fileurl: nsurl? documentsdirectory = nsfilemanager.defaultmanager().urlsfordirectory(.documentdirectory, indomains: .userdomainmask).last! fileurl = documentsdirectory!.urlbyappendingpathcomponent("file.txt") if (fileurl!.checkresourceisreachableandreturnerror(nil)) { print("file exist") }else{ print("file doesnt exist") nsdata().writetourl(fileurl!,atomically:true) }
now can access fileurl