2

I'm actually not sure whether I'm not able to read or unable to write. I'm pretty sure writing works. I'm using Swift.

Here are my project files.

I'm trying to write an array of arrays to a file, and then read that array of arrays when the app launches. Here's my code for writing and reading, respectively. listOfTasks is my array of arrays variable.

Writing:

let cocoaArray : NSArray = listOfTasks
cocoaArray.writeToFile(String(fileURL), atomically: true)

Reading:

listOfTasks = NSArray(contentsOfFile: String(fileURL)) as! [Array<String>]

fileURL:

let documentsDirectory = NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask).last

let fileURL = documentsDirectory!.URLByAppendingPathComponent("file.txt")

I'm happy to provide additional information as necessary. Thanks in advance!

atirit
  • 1,492
  • 5
  • 18
  • 30
  • What type of objects are in the array? Show how you create the value for `fileURL`. – rmaddy Jun 02 '16 at 04:20
  • whats your fileURL? – LuKenneth Jun 02 '16 at 04:26
  • try `NSArray(contentsOfURL: fileURL)` – Leo Dabus Jun 02 '16 at 04:46
  • You know that `[Array]` is equivalent to `Array>`, which is equivalent to `[[String]]`, right? – redent84 Jun 03 '16 at 13:53
  • @redent84 Actually, no, I didn't. I learned something new! I'm pretty new to Swift, so excuse my not drawing connections. – atirit Jun 03 '16 at 13:55
  • I guess that you're saving a `[String]` and trying to retrieve a `[[String]]` and therefore is failing. – redent84 Jun 03 '16 at 13:56
  • I took a look at your code, you're missing some basics. For example, `var listOfTasks = [[String(),String(),String()]]` creates a `[[String]]` (array of array of strings) with **one** element, which is an array containing **three empty strings**. That's probably not what you're expecting. Probably `var listOfTasks = [[String]]()` is what you were trying. – redent84 Jun 03 '16 at 14:12
  • @redent84 No, that's what I want. If you downloaded my files you'll see the three fields in my prototype cell. The three strings go into those. – atirit Jun 03 '16 at 15:53
  • I downloaded your files, and saw that you're instantiating an array with ONE (that it's an array of three empty strings `String()` ) element and then you're appending more elements. I don't think that you wanted that first element. If you wanted an array of three strings, you probably want to use an array of string tuples: `[(String, String, String)]`. – redent84 Jun 03 '16 at 16:08

1 Answers1

3

Not sure where's exactly your problem, but here's a full working Playground sample:

let fileUrl = NSURL(fileURLWithPath: "/tmp/foo.plist") // Your path here
let array = [["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"], ["One", "Two", "Three"]] // Your array of arrays here

// Save to file
(array as NSArray).writeToURL(fileUrl, atomically: true)

// Read from file
let savedArray = NSArray(contentsOfURL: fileUrl) as! [[String]]

print(savedArray)
redent84
  • 18,901
  • 4
  • 62
  • 85