


What it does is let us effectively ignore UserDefaults entirely, and just use rather than like this: struct ContentView: View private var tapCount = 0Īgain, there are three things I want to point out in there: Now, I mentioned that SwiftUI provides an property wrapper around UserDefaults, and in simple situations like this one it’s really helpful. There used to be a way of forcing updates to be written immediately, but at this point it’s worthless – even if the user immediately started the process of terminating your app after making a choice, your defaults data would be written immediately so nothing will be lost. How much time is another number we don’t know, but a couple of seconds ought to do it.Īs a result of this, if you tap the button then quickly relaunch the app from Xcode, you’ll find your most recent tap count wasn’t saved. They don’t write updates immediately because you might make several back to back, so instead they wait some time then write out all the changes at once. Second, it takes iOS a little time to write your data to permanent storage – to actually save that change to the device. With Booleans, for example, you get back false if boolean(forKey:) can’t find the key you asked for, but is that false a value you set yourself, or does it mean there was no value at all? Sometimes having a default value like 0 is helpful, but other times it can be confusing. First, what happens if we don’t have the “Tap” key set? This will be the case the very first time the app is run, but as you just saw it works fine – if the key can’t be found it just sends back 0.

There are two things you can’t see in that code, but both matter. Go ahead and give the app a try and see what you think – you ought to be able tap the button a few times, go back to Xcode, run the app again, and see the number exactly where you left it. Notice how that uses exactly the same key name, which ensures it reads the same integer value. Speaking of reading the data back, rather than start with tapCount set to 0 we should instead make it read the value back from UserDefaults like this: private var tapCount = (forKey: "Tap") This key is case-sensitive, just like regular Swift strings, and it’s important – we need to use the same key to read the data back out of UserDefaults. We attach a string name to this data, in our case it’s the key “Tap”.There is a single set() method that accepts any kind of data – integers, Booleans, strings, and more.For example, if you want to share defaults across several app extensions you might create your own UserDefaults instance. This is the built-in instance of UserDefaults that is attached to our app, but in more advanced apps you can create your own instances.
CHECK IF VALUE ALREADY EXISTS IN USER DEFAULTS SWIFT CODE
In just that single line of code you can see three things in action: So, add this after the tapCount += 1 line: (self.tapCount, forKey: "Tap") To make that happen, we need to write to UserDefaults inside our button’s action closure. Here’s a view with a button that shows a tap count, and increments that count every time the button is tapped: struct ContentView: View private var tapCount = 0Īs this is clearly A Very Important App, we want to save the number of taps that the user made, so when they come back to the app in the future they can pick up where they left off. Even better, SwiftUI can often wrap up UserDefaults inside a nice and simple property wrapper called – it only supports a subset of functionality right now, but it can be really helpful.Įnough chat – let’s look at some code. UserDefaults is perfect for storing things like when the user last launched the app, which news story they last read, or other passively collected information. Tip: If you’re thinking “512KB? How much is that?” then let me give you a rough estimate: it’s about as much text as all the chapters you’ve read in this book so far. To give you at least an idea, you should aim to store no more than 512KB in there. There is no specific number attached to “a small amount”, but everything you store in UserDefaults will automatically be loaded when your app launches – if you store a lot in there your app launch will slow down. One common way to store a small amount of data is called UserDefaults, and it’s great for simple user preferences. Most users pretty much expect apps to store their data so they can create more customized experiences, and as such it’s no surprise that iOS gives us several ways to read and write user data.
