Apologies
You must be signed in to watch this lesson.
Persistence with UserDefaults NSCoding
Podcasts
Many of you are probably aware that you can save basic value types into UserDefaults. For example, you can save these types: Int, String, NSNumber, Array, etc. The question now is: how would you save a Podcast into UserDefaults? Well, the secret here is to conform to the NSCoding protocol and tell the system what exactly to save. Once our save is done, we also have to retrieve it from UserDefaults somehow. The process in which this happens is to decode the blob of data that we associated to the key we used to perform the save. All that and more in today's lesson.

Comments (5)
tomxue
6 years ago
There's an easier way, you can conform Podcast to Codable with no additional work, using JSONEnconder to encode the podcast to data, and save it in the user defaults. Later when you want to retrieve it, get the data object from UserDefaults, and use JsonDecoder to transform the data to class directly. It takes 4 lines of code to do that, and you don't have to write any new protocol methods or write all the raw string keys.
Brian Voong
6 years ago
Kenny Ho
5 years ago
Hi Tomxue, can you share that approach? I was following Brian, but I got an error where it said "property with type String? cannot override a property with type String". The problem came from sub-classing it as NSObject
Parlad
5 years ago
//MARK: - Helper function var key = "favrioteSaved" @objc private func handleFavroiteSave(){ if let podcast = podcast { do{ let data = try JSONEncoder().encode(podcast) UserDefaults.standard.set(data, forKey: key) print("sucess") }catch let error{ print("Failed to save: Error: \(error)") } } } @objc func handleFetch(){ do{ if let data = UserDefaults.standard.data(forKey: key){ let podcast = try JSONDecoder().decode(Podcast.self, from: data) print(podcast) } }catch let error { print("Failed to save: Error: \(error)") } } this is my approach.
Katlego
5 years ago
What a pro, thank you.
Kelvin Chen
4 years ago
But the archive and unarchive func were Deprecated now. I just replace with NSSecureCoding, the new method needs to using 'try' syntax... T_T
LaurieHouse
4 years ago
Also running into this error. Hoping to find a fix online somewhere if Brian can't get back to us in time.
Brian Voong
4 years ago
LaurieHouse
4 years ago
Do you replace the NSKeyedArchiver with Decodables later in the lessons? I tried getting around the depreciation by using do/catch do { let data = try NSKeyedArchiver.archivedData(withRootObject: sign, requiringSecureCoding: false) as Data UserDefaults.standard.set(data, forKey: favoritedSignsKey) UserDefaults.standard.synchronize() } catch { //error handling } but am getting a Thread 1: signal SIGABRT at my 'let data' line.
moohammed
3 years ago
Thanks bro, but how can i list all the podcasts in the fav controller? i mean now in handleFetch what is the key ? that will fetch each podcast ?
juliolocoh
6 years ago
Brian can you please in the course the top 20 of podcast i mean the podcasts that are the most listen to thanks
tobitech
5 years ago
Hello Brian, I decided to use Core Data for saving my episodes, please how can I make PlayerViewDetails accept two kinds of episodes with different datatypes, JSONEpisode and Episode (of NSManagedObject) and the playlists as well?
Kenny Ho
5 years ago
I'm working on a project that has a "description" in Podcast. I'm getting a error "property with type String? cannot override a property with type String". It turns out that NSObject has its own property called description. How do I go about renaming "description" as "decript" but when searching through the JSON, it knows to that "descrip" refers to "description"? I'm having trouble articulating the problem so I'm having difficulties searching for the solution online.
Brian Voong
5 years ago
Kenny Ho
5 years ago
Thank you so much!
Bekzod Rakhmatov
5 years ago
Hello Brian! I am going to ask a simple question that is not related to this topic. I have been struggling saving images and uploading to a server. I have some images taken on my app and I just need to rename the images with custom preference. For some reasons image's name is autogenerated by app itself. If you have any experience could you guide me how I can change image name in my app and collect all in a file and zip it and upload to server. At least please help me how to rename images on iOS apps. Thank you for your time!
Brian Voong
5 years ago
Bekzod Rakhmatov
5 years ago
Thank you for your respond. I have seen Zendesk Support SDK in our app and we can pick images from phone and we can give it our own name so when user sends the image in our support page we will receive as we give the image name. I have been looking for this for a long time now so there is no answer that is why I asked. Cheers!
HELP & SUPPORT