4

Here are my files.

How can I write this array:

var listOfTasks = [["Hi", "Hello", "12:00"],["Hey there", "What's up?", "3:17"]]

to a .txt file (file.txt)? I know there are other questions about this, but they're in other languages (not Swift). I'd like for it to work on an actual iPhone. I'm happy to provide additional information if necessary. I'm new to Swift, so excuse my question if it seems too simple. NOTE: I am asking specifically about arrays containing arrays.

Thanks in advance!

4
  • where do you want this text file to live? on your computer? on the phone? Commented Jun 2, 2016 at 18:40
  • @LukePatterson On the phone. Why is this question getting downvoted? Commented Jun 2, 2016 at 18:40
  • Duplicate : stackoverflow.com/questions/24097826/… Commented Jun 2, 2016 at 19:05
  • No. That question discusses strings. I'm specifically asking about arrays of arrays. Commented Jun 2, 2016 at 20:39

2 Answers 2

8

Already answered you in your other question:

Here's a full working Playground sample:

let fileUrl = NSURL(fileURLWithPath: "/tmp/foo.plist") // Your path here
let listOfTasks = [["Hi", "Hello", "12:00"], ["Hey there", "What's up?", "3:17"]]

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

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

print(savedArray)
Sign up to request clarification or add additional context in comments.

1 Comment

To be clear - the NSArray.write(to:atomically:) method write the array in plist structure, rather than a plain newline-separated text file. Which means this method is not suitable for exporting XCFileList.
2

You should use NSKeyedArchiver/NSKeyedUnarchiver to read/write your array as a property list file instead of a text file. You can save your file to the preferences folder inside the Library folder:

let preferencesDirectory =  try! NSFileManager().URLForDirectory(.LibraryDirectory, inDomain: .UserDomainMask, appropriateForURL: nil, create: true).URLByAppendingPathComponent("Preferences", isDirectory: true)

let listOfTasksURL = preferencesDirectory.URLByAppendingPathComponent("listOfTasks.plist")

var listOfTasks: [[String]] {
    get {
        return NSKeyedUnarchiver.unarchiveObjectWithFile(listOfTasksURL.path!) as? [[String]] ?? []
    }
    set {
        NSKeyedArchiver.archiveRootObject(newValue, toFile: listOfTasksURL.path!)
    }
}

If you would like to test it in a playground file you will need to save it to the documents directory:

let documentsDirectory =  try! NSFileManager().URLForDirectory(.DocumentDirectory, inDomain: .UserDomainMask, appropriateForURL: nil, create: true)

let listOfTasksURL = documentsDirectory.URLByAppendingPathComponent("listOfTasks.plist")

var listOfTasks: [[String]] {
    get {
        return NSKeyedUnarchiver.unarchiveObjectWithFile(listOfTasksURL.path!) as? [[String]] ?? []
    }
    set {
        NSKeyedArchiver.archiveRootObject(newValue, toFile: listOfTasksURL.path!)
    }
}

listOfTasks =  [["Hi", "Hello", "12:00"],["Hey there", "What's up?", "3:17"]]

listOfTasks // [["Hi", "Hello", "12:00"], ["Hey there", "What's up?", "3:17"]]

2 Comments

This code seems to be for an array of strings; I have an array that contains arrays, which in turn contain multiple strings per sub-array. Or maybe I'm not implementing the code correctly. EDIT: Sorry, my bad, wasn't very clear. Or does this code work for multiple?
@ghostpotato done this doesn't change much of the code

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.