简体   繁体   中英

Most Efficient way to Store Data in Swift

I am currently working on a Swift project and I would like to save permanent user data in the following manner:

USERNAME - List of Strings (String array)

This so that when I search for a Username, I am either given a nil return (if the username does not exist), or a list of Strings that I can iterate through. I know of a few methods of saving user data, however, as I am new to Swift, I do not know all methods of permanently save user data. Nor do I know which ones best suit my needs.

Something like this…

let people: [String: [String]] = ["Jim": ["bread", "fruit", "meat"],
              "Alan": ["pears", "peas", "turnip"],
              "Sue": ["cabbage", "rice", "bblueberries"]]

let documentsUrl = FileManager.default.urls(for: .documentDirectory, in: FileManager.SearchPathDomainMask.userDomainMask).last!
let fileUrl = documentsUrl.appendingPathComponent("myfile")

(people as NSDictionary).write(to: fileUrl, atomically: true)

and then…

let people = NSDictionary(contentsOf: fileUrl) as! [String: [String]]

For system urls… https://developer.apple.com/reference/foundation/filemanager/1407726-urls

NSDictionaryhttps://developer.apple.com/reference/foundation/nsdictionary?language=swift

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM