Reputation: 479
I got this code from the documentation, I can't find the path to my file, I need to copy the "contacts.db" file from Supporting Files Folder to the app in the device not in the simulator for offline use.
func copyItemAtPath(_ srcPath: String,
toPath dstPath: String,
error error: NSErrorPointer) -> Bool
srcPath = The path to the file or directory you want to move. This parameter must not be nil.
dstPath = The path at which to place the copy of srcPath. This path must include the name of the file or directory in its new location. This parameter must not be nil.
error = On input, a pointer to an error object. If an error occurs, this pointer is set to an actual error object containing the error information. You may specify nil for this parameter if you do not want the error information.
Any help is highly appreciated. :)
Upvotes: 2
Views: 6025
Reputation: 71854
You can just drag and drop "contacts.db" into Project Navigator and after that you can find a path of that file this way:
let sourcePath = NSBundle.mainBundle().pathForResource("contacts", ofType: "db")
after that you can copy that file into document folder of your app and for that you need that document folder path:
let doumentDirectoryPath = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true).first as! String
let destinationPath = doumentDirectoryPath.stringByAppendingPathComponent("contacts1.db")
now you have sourcePath
and destinationPath
so you can copy that file into document folder:
NSFileManager().copyItemAtPath(sourcePath!, toPath: destinationPath, error: &error)
and if you want to check for error you can do it this way:
var error : NSError?
NSFileManager().copyItemAtPath(sourcePath!, toPath: destinationPath, error: &error)
if let error = error {
println(error.description)
}
If you are getting your sourcePath
nil then goto Targets->YouApp->Build Phases->copy Bundle Resources and add your file there.
Hope this will help.
Upvotes: 7