Search code examples
iosswiftnsdatensdocumentdirectory

Seems like my Text file isn't storing persistently in Documents Directory


NOTE: Possibly Resolved: Was missing ".path!" to documentsUrl.path!.stringByAppendingPathComponent(path)

I am saving a text file to Applications Documents Directory. Using

var path = ""
let date = NSDate()
let file = "\(date).txt"
let joined = "\n".join(ActionArray)
if let dirs : [String] = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.AllDomainsMask, true) as? [String] {
    let dir = dirs[0]
    path = dir.stringByAppendingPathComponent(file);
    println(path)

    joined.writeToFile(path, atomically: false, encoding: NSUTF8StringEncoding, error: nil);
}

println path

/Users/dustin/Library/Developer/CoreSimulator/Devices/5EA056FC-114D-4C98-9B35-29B46760BE3A/data/Containers/Data/Application/2AB9FD4A-25AB-4385-9CFB-A66AEFA8C1A3/Documents/2015-05-16 08:24:08 +0000.txt

Then store path and some other data to an Entity, which will be used to call back the text file later.

When I do a fetchRequest on the entity and print out the array I get

( " (entity: Entity; id: 0xd000000000040000 ; data: {\n date = \"2015-05-16 08:18:20 +0000\";\n path = \"/Users/dustin/Library/Developer/CoreSimulator/Devices/5EA056FC-114D-4C98-9B35-29B46760BE3A/data/Containers/Data/Application/4A9C17AB-57C1-47CB-B9B1-E0E5CA494B4E/Documents/2015-05-16 08:24:08\";\n})" )

the path is now missing the " +0000". When I call on the path from the Entity.path, my text file is found...BUT when I completely close app (home-swipe-^), the text file search...

let fileContent = String(contentsOfFile: path, encoding: NSUTF8StringEncoding, error: nil)!
var fileContentArr = split(fileContent) {$0 == "\n"}

I get

fatal error: unexpectedly found nil while unwrapping an Optional value


What am I doing wrong to write a persistent text file to draw upon later regardless if App reset?


EDIT: I've taken advice to notice directory path changes between App sessions, So I've instead stored file name to Entity and get Documents Path at read time. I still get error.

I'm doing some debugging and my println's are as follows

let documentsUrl =  NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask)[0] as! NSURL

    // now lets get the directory contents (including folders)
    if let directoryContents =  NSFileManager.defaultManager().contentsOfDirectoryAtPath(documentsUrl.path!, error: nil) {
        println(directoryContents)
        println(documentsUrl.path!)
    }

[2015-05-17 04:25:48 +0000.txt, 2015-05-17 04:31:07 +0000.txt, iPro_Poker_HH_swift.sqlite, iPro_Poker_HH_swift.sqlite-shm, iPro_Poker_HH_swift.sqlite-wal]

/Users/dustindobrilovic/Library/Developer/CoreSimulator/Devices/5EA056FC-114D-4C98-9B35-29B46760BE3A/data/Containers/Data/Application/7CD25462-947C-40AA-97DB-4A845FED1451/Documents

My Entity Fetch array println()

( " (entity: Hand; id: 0xd000000000080000 ; data: {\n path = \"2015-05-17 04:31:07 +0000.txt\";\n})" )

I then combine documentsUrl.path! with path from entity to get println(stringDocumentsURL)

file:/Users/dustindobrilovic/Library/Developer/CoreSimulator/Devices/5EA056FC-114D-4C98-9B35-29B46760BE3A/data/Containers/Data/Application/7CD25462-947C-40AA-97DB-4A845FED1451/Documents/2015-05-17 04:31:07 +0000.txt

This is where Im getting a warning which is very next line.

let fileContent = String(contentsOfFile: stringDocumentsURL, encoding: NSUTF8StringEncoding, error: nil)! 

fatal error: unexpectedly found nil while unwrapping an Optional value

>


Solution

  • Path changes across launches. Try to persist only the relative part of the path and reconstruct the absolute path before accessing it.