0

So I want to figure out where my swift IOS app is storing its SQLite db file. I know that the applicationsDocumentsDirectory stores the directory. I need to find a way to print it in the console or NSLog it. I am new to swift and IOS development so I'm having trouble here. I tried just calling the variable in another class and also just NSLogging it within the clojure with no success. Any ideas?

Here is the variable.

    lazy var applicationDocumentsDirectory: NSURL = {
    // The directory the application uses to store the Core Data store file. This code uses a directory named "arux-software.onsite_childcare" in the application's documents Application Support directory.
    let urls = NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask)
    return urls[urls.count-1] as NSURL
    }()
Boid
  • 1,161
  • 1
  • 11
  • 21

2 Answers2

2

There is an easier way to get the sqlite location:

println(NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0] as NSString)

gives output with no time stamp like this:

/Users/tooti/Library/Developer/CoreSimulator/Devices/AB5B3350-F891-420B-88D5-E8F8E578D39B/data/Containers/Data/Application/38FBDC42-0D09-4A10-A767-17B576882117/Documents

And

NSLog(NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0] as NSString )

gives output with a time stamp, like this:

2015-01-23 23:31:57.373 RAACTutor[6551:140531] /Users/tooti/Library/Developer/CoreSimulator/Devices/AB5B3350-F891-420B-88D5-E8F8E578D39B/data/Containers/Data/Application/38FBDC42-0D09-4A10-A767-17B576882117/Documents
Community
  • 1
  • 1
HalR
  • 11,411
  • 5
  • 48
  • 80
0

You can do it in two ways.

First Way

1. we can get reference to the application document directory using NSSearchPathForDirectoriesInDomains() method.

let documentsPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).last! as String

The constant .documentDirectory says we are looking for Document directory.

The constant .userDomaininMask to restrict our search to our application's sandbox.

Prefered way to refer to a file or directory is with a URL.

let documentsURL = URL(string: documentsPath)!
print(documentsURL)

Second Way

2. using FileManager.default.url function.

 let fileManager = FileManager.default

    do {
        let documentsURL = try fileManager.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: true)
        print(documentsURL)

    } catch let error as NSError {
        print("Could not save \(error), \(error.userInfo)")
    }
Ashok R
  • 19,892
  • 8
  • 68
  • 68