How can I save an array of strings?

I'm new to app development and I try to save an array of strings to stay the same after a restart. I want to easily append the array, to remove some strings and to use the array as an normal array. What is the best way to do this?

Answered by DTS Engineer in 341496022

If you have not a large number, it is simple to save in

UserDefaults
.

I generally recommend that you use user defaults for preference-like data. That is, the user would only be moderately grumpy if the data goes away. If these strings are important to the user, it’s probably best to save them in their own file.

Fortunately that’s pretty simple:

var fileURL: URL = {
    let docDir = try! FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: true)
    return docDir.appendingPathComponent("MyStrings.plist")
}()

func save(strings: [String]) throws {
    let d = try PropertyListEncoder().encode(strings)
    try d.write(to: self.fileURL, options: [.atomic])
}

func load() throws -> [String] {
    let d = try Data(contentsOf: self.fileURL)
    return try PropertyListDecoder().decode(Array<String>.self, from: d)
}

Two things:

  • Note all of the error handling. This is one of the key things you miss when you use user defaults.

  • Storing your data in a property list is fine for up to a few hundred strings. If you need to store a few thousand strings, it’s time to look for better solutions.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

You can save data in several ways.


If you have not a large number, it is simple to save in userDefaults.


See details here:

https://stackoverflow.com/questions/41783273/swift-store-array-with-userdefaults

Accepted Answer

If you have not a large number, it is simple to save in

UserDefaults
.

I generally recommend that you use user defaults for preference-like data. That is, the user would only be moderately grumpy if the data goes away. If these strings are important to the user, it’s probably best to save them in their own file.

Fortunately that’s pretty simple:

var fileURL: URL = {
    let docDir = try! FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: true)
    return docDir.appendingPathComponent("MyStrings.plist")
}()

func save(strings: [String]) throws {
    let d = try PropertyListEncoder().encode(strings)
    try d.write(to: self.fileURL, options: [.atomic])
}

func load() throws -> [String] {
    let d = try Data(contentsOf: self.fileURL)
    return try PropertyListDecoder().decode(Array<String>.self, from: d)
}

Two things:

  • Note all of the error handling. This is one of the key things you miss when you use user defaults.

  • Storing your data in a property list is fine for up to a few hundred strings. If you need to store a few thousand strings, it’s time to look for better solutions.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"
How can I save an array of strings?
 
 
Q