How to Save Data with UserDefaults - Swift

preview_player
Показать описание
In this tutorial we walk through the basics of saving data using UserDefaults is Swift. We'll discuss what types of data you can save, some of the security issues involved, and how to make it all happen. In this example, we'll store a light or dark mode preference, as well as the name of a pet as a String. This tutorial was done in Swift 4.2 and Xcode 10.

If you like my teaching style, I've released my first iOS dev course which is all about the take-home project job interview for iOS developers.

Source Code (Xcode 10, Swift 4.2):

How I Became an iOS Developer:

Check out my podcast, iOS Dev Discussions:

Book and learning recommendations that help out the channel if you decide to purchase (Affiliate Links):

Ray Wenderlich Books:

Ray Wenderlich Videos:

Paul Hudson's Hacking With Swift:

Learn Advanced Swift Here:

Links to my Developer & YouTube Setup

Personal Links:

Twitter:

Instagram:
@seanallen_dev

Website:
Рекомендации по теме
Комментарии
Автор

Just as I'm about to implant UserDefaults again your video pops up. Great timing. Needed the refresher

YOAMOD
Автор

When I first saw the title "save data" I thought you're going to "save user data" and almost started writing a comment about why doing that is a very bad practice, but then I realized you are talking about just saving user preferences :) When I just started developing for MacOS back in 2005 I quickly learned what should be saved in UserDefaults and what should be not. There is a well-known practice in MacOS world that if you need to reset you app to defaults the best way to do that is just to delete your preferences plist file, that way you simulate fresh start of your app on your system, and no real user data/documents should be lost by doing that. That makes a simple rule: everything stored in UserDefaults should be discardable without the risk of user's personal data loss.

ArtemDzhadzha
Автор

This tutorial was just what i needed right now. Btw, I find your way to use structs to prevent typos very useful.

mccooldudehotashell
Автор

Really Nice example and clean code with good explanation of what to save into that especially, waiting for database and core data examples.

govindsuryawanshi
Автор

I knew this concept very well however it's always good to see your videos as hoping to learn something more and I do always :) Thanks for your time brining this quality videos for any expertise level iOS developer.

hemangshah
Автор

I really like your videos! they are simple, easy to follow and clear... thank you very much for your help!

geovannichavez
Автор

Great example and really well explained - more of these please Sean!

RichardRichardVC
Автор

Great points about what to save and what not to save! Thanks Sean

Eumaden
Автор

Wow Great video. It is just a little too fast paced video for a newbie like me. I had to pause and rewind to get the concept digested. But no worries you explained it very well. Thanks for putting out this video.

nileshjdarji
Автор

Excellent as always and very timely. Thank you.

forrestanderson
Автор

Love the videos, would it be possible to do a tutorial in the future about how to scan QR/Barcodes for inventory management? Like something that adds and deletes from either a simple SQL table or core data? Thanks in advance!

krete
Автор

Great tutorial Sean! It would be great to follow this up with a tutorial on how to save data securely.

rickywitherspoon
Автор

i needed of this tutorial, it's great thanks

otaviosouza
Автор

Always clear and sharp as usual. Great job Sean Allen

cinquain
Автор

Thank you, Sean! Yours tutorials are great!! 👍🤠

macasasa
Автор

Hello, thank you for the amazing tutorial and neat code provided! I have a question, I want to do the same exact thing (changing background color) but for my entire app pages. I have tried keeping the checkForStylePreference() in the viewDidLoad()of another page but it did not recognize it. I copy pasted the whole checkForStylePreference() but still other pieces of code were missing. Is the only way to do it is by copy pasting all of the methods of the viewController in all App pages? Or there is a much simpler way as a believe to reduce amount of code? Please advise I am new to Swift.

nooral-awadi
Автор

Hi, first of all awesome video! second - do we still need to do nil coalescing even tho the default value of a String is an empty String? I mean the program wont crash, just want to know what's the right thing to do and what good programers do :)

aviadsabag
Автор

wow man, You always come with awesome topic with nice explanation

JunaidKhan-nytu
Автор

Here we go with another awesome tutorial! ❤👍✌

hoxorious
Автор

Thanks Sean for another great video.

One request, can you make one video where you can show how to store the login status of the user. For eg if user is logged in he'll directly go to home screen else show the login screen.

Also if that app has on boarding screens which user can see only once when the fresh app is installed.

Again thanks for your all the efforts for making these videos.

iOSTechie
join shbcf.ru