DEV Community

Cover image for #100DaysOfSwift : Project 12
KeeyboardKeda
KeeyboardKeda

Posted on

#100DaysOfSwift : Project 12

In this project I learned about UserDefaults, NSCoding and reviewed the Codable protocol. UserDefaults lets you store program settings, user settings and runs as soon as the app loads.

Key Takeaways:

  1. When using the nil coalescing operator you want to see if the code on the left side of the operator "??" has a value and if not then use the value on the right side of the operator. Example,

let array = defaults.object(forkey: "SavedArray") as? [String] ?? String

If there is a value for the key SavedArray then set it as the value for the constant array, if not then set it to be a new string array.

  1. NSKeyedArchiver.archivedData(withRootOgbject:, requiringSecureCoding:) encodes(writes) an object graph with the given root object into a data representation, optionally requiring secure coding. An object graph is your object, plus other objects it refers to and so on.

  2. NSKeyedUnarchiver.unarchivTopLevelObjectWithData() decodes(reads) archived object graph and returns the object root.

4.Using "if let" and "try?" helps to save data only when converting into to a Data object was successful.

Discussion (0)