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
    }()

2 Answers 2

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
Sign up to request clarification or add additional context in comments.

Comments

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)")
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.