Learn how to use UserDefaults to save data in your app.
Enjoy this info? Visit my Patreon page to support educators and artists.
My YouTube Channel
Need help with SwiftUI? Check out my products at: Big Mountain Studio Website
Showing posts with label nsuserdefaults. Show all posts
Showing posts with label nsuserdefaults. Show all posts
Monday, January 30, 2017
Sunday, January 29, 2017
How to make an Onboarding Screen (iOS, Xcode 8, Swift 3)
Show an onboarding (intro) screen the first time the user uses your app. Keep track if they saw it or not so you know if it should come first.
Thursday, May 12, 2016
Persisting a Class with UserDefaults
In a previous post I showed how to persist single values. This post shows how to save objects/classes of data.
You app needs to take data from memory and store it in a file. This is what the NSKeyedArchiver does. It "archives" the data or writes it for future use.
To restore the archived data (file) back into memory you use the NSKeyedUnarchiver. This will restore your data back into a class you can then use in your code.
(Swift 3)
class SaveClassToUserDefaultsVC: UIViewController {
@IBOutlet weak var NameTextField: UITextField!
@IBOutlet weak var AgeTextField: UITextField!
let defaults = UserDefaults.standard
override func viewDidLoad() {
super.viewDidLoad()
GetPerson()
}
@IBAction func SavePerson(sender: AnyObject) {
let person = Person(name: NameTextField.text!, age: AgeTextField.text!)
let personData = NSKeyedArchiver.archivedData(withRootObject: person)
defaults.set(personData, forKey: "personData")
}
func GetPerson() {
if let personData = defaults.data(forKey: "personData") {
if let person = NSKeyedUnarchiver.unarchiveObject(with: personData) as? Person {
NameTextField.text = person.name
AgeTextField.text = person.age
}
}
}
}
What is NSKeyedArchiver and NSKeyedUnarchiver?
If you are familiar with other languages this is similar to serializing and deserializing.You app needs to take data from memory and store it in a file. This is what the NSKeyedArchiver does. It "archives" the data or writes it for future use.
To restore the archived data (file) back into memory you use the NSKeyedUnarchiver. This will restore your data back into a class you can then use in your code.
Setup You Class To Be "Archivable"
There is some setup work you have to do to your class before it can be archived and unarchived.
class Person: NSObject, NSCoding {
var name = ""
var age = ""
required init(name: String, age: String) {
self.name = name
self.age = age
}
required init(coder decoder: NSCoder) {
self.name = decoder.decodeObject(forKey: "name") as? String ?? ""
self.age = decoder.decodeObject(forKey: "age") as? String ?? ""
}
func encode(with coder: NSCoder) {
coder.encode(self.name, forKey: "name")
coder.encode(self.age, forKey: "age")
}
}
(Swift 3)
Tuesday, May 10, 2016
Easiest Way to Persist Data
If your needs are simple, consider this option before moving on to other storage methods.
(Swift 3.0)
Saving
@IBOutlet weak var nameTextField: UITextField! ... UserDefaults.standard.setValue(nameTextField.text, forKey: "name")
Retrieving
if let name = UserDefaults.standard.value(forKey: "name") as? String {
nameTextField.text = name
}
Tips
Save some typing and create a variable to access the defaults:- let defaults = UserDefaults.standard
(Swift 3.0)
Subscribe to:
Posts (Atom)
SwiftUI Search & Filter with Combine - Part 3 (iOS, Xcode 13, SwiftUI, 2...
In part 3 of the Searchable video series, I show you how to use Combine in #SwiftUI for the search and filter logic connected to the searcha...
-
This post visually shows the stack view's Alignment and Distribution property settings. There is also a section on resolving auto layou...
-
I couldn't find a tutorial on this so this is my documented journey into trying to figure out how to implement OAuthSwift into an iOS p...
-
I had someone ask about aligning text within a TextField view and I didn't have a page in my book for this but added one just now. ...