ios - How to find a correct path in Swift -


i got code documentation, can't find path file, need copy "contacts.db" file supporting files folder app in device not in simulator offline use.

func copyitematpath(_ srcpath: string,          topath dstpath: string,           error error: nserrorpointer) -> bool 

srcpath = path file or directory want move. parameter must not nil.

dstpath = path @ place copy of srcpath. path must include name of file or directory in new location. parameter must not nil.

error = on input, pointer error object. if error occurs, pointer set actual error object containing error information. may specify nil parameter if not want error information.

any highly appreciated. :)

you can drag , drop "contacts.db" project navigator , after can find path of file way:

let sourcepath = nsbundle.mainbundle().pathforresource("contacts", oftype: "db") 

after can copy file document folder of app , need document folder path:

let doumentdirectorypath = nssearchpathfordirectoriesindomains(.documentdirectory, .userdomainmask, true).first as! string let destinationpath = doumentdirectorypath.stringbyappendingpathcomponent("contacts1.db") 

now have sourcepath , destinationpath can copy file document folder:

nsfilemanager().copyitematpath(sourcepath!, topath: destinationpath, error: &error) 

and if want check error can way:

var error : nserror?     nsfilemanager().copyitematpath(sourcepath!, topath: destinationpath, error: &error)     if let error = error {         println(error.description)     } 

if getting sourcepath nil goto targets->youapp->build phases->copy bundle resources , add file there. hope help.


Comments

Popular posts from this blog

android - MPAndroidChart - How to add Annotations or images to the chart -

javascript - Add class to another page attribute using URL id - Jquery -

firefox - Where is 'webgl.osmesalib' parameter? -