FAQ: How do I open a database stored as a resource of my application?

This commit is contained in:
Gwendal Roué committed 2016-03-03 14:17:47 +01:00
1 parent 9aa629d36e
commit cfb987e7e3
1 file changed
+3 -3
+3 -3
View File
@@ -2383,12 +2383,12 @@ FAQ
```swift
let fm = NSFileManager.defaultManager()
let documentsURL = try! fm.URLForDirectory(.DocumentDirectory, inDomain: .UserDomainMask, appropriateForURL: nil, create: true)
let dbResourceURL = NSBundle.mainBundle().URLForResource("db", withExtension: "sqlite")
let dbResourceURL = NSBundle.mainBundle().URLForResource("db", withExtension: "sqlite")!
let dbURL = documentsURL.URLByAppendingPathComponent(dbResourceURL.lastPathComponent!)
if !fm.fileExistsAtPath(dbURL.path) {
if !fm.fileExistsAtPath(dbURL.path!) {
try! fm.copyItemAtURL(dbResourceURL, toURL: dbURL)
}
let dbQueue = DatabaseQueue(dbURL.path)
let dbQueue = DatabaseQueue(path: dbURL.path!)
```