Apologies
You must be signed in to watch this lesson.
Save Favorites List and Long Press Delete
Podcasts
Being able to persist a single Podcast in our program is nice, but we really need to save a list of podcasts instead. In today's lesson, we discuss how to easily go from saving a single Podcast to saving an array such as [Podcast]. Once done with this implementation, we render out our favorited podcasts in our FavoritesController collection view. Finally the last task is to show how we can delete a podcast by using a UILongPressGestureRecognizer. Enjoy.

Comments (4)
xuano
5 years ago
Hey Brian, I realized there is a bug within the deletePodcast method in the UserDefaults extension. If we favorite several podcasts with the same artistName (e.g. NPR), whenever we try to delete one of the podcasts, it will delete all the podcasts with the same artistName, because the predicate of the filter function will return false. One posible fix, in case anyone is interested, is as follows: let filteredPodcasts = podcasts.filter { p -> Bool in return p.trackName != podcast.trackName || (p.trackName == podcast.trackName && p.artistName != podcast.artistName) }
mohit1007
5 years ago
Hey brian.. For the challenge. To remove the podcast from UserDefaults, I am simply "overwriting" the dictionary with the removed podcast array like following let data = NSKeyedArchiver.archivedData(withRootObject: self.podcasts) UserDefaults.standard.set(data, forKey: UserDefaults.favoritePodcastKey) Is this hacky solution or if there is a better way out?
nicolehinckley
5 years ago
That's how I've done it. Seems fine to me!
nicolehinckley
5 years ago
In the next video he shows how he set it up in the User Defaults extension though. Yours is a simple way to go about it, but his is definitely more "proper".
yuiso
5 years ago
Hey Brain, The way of using NSKeyedArchiver and unarchiver is changed in iOS12. Can you please update the project code so we can follow the new version?
Brian Voong
5 years ago
marknelpogi
5 years ago
Hi Brian, I downloaded the file but I think it is still not updated to Swift 4.2.
marknelpogi
5 years ago
Hi Brian, I downloaded the file but I think it is still not updated to Swift 4.2.
Brian Voong
5 years ago
LaurieHouse
4 years ago
Hi Brian, I'm running into the same issue. The project download is updated for Swift 4.2 but not for iOS 12. The project still runs and builts, but is throwing the warning that it's depreciated. So not the end of the world, just mildly world ending. haha.
Kritbovorn Taweeyossak
4 years ago
@objc fileprivate func handleSaveFavorite() { print("@@@ Save Favorite") guard let podcast = podcast else { return } // Change [ podcast ] into [ Data ] do { let data = try NSKeyedArchiver.archivedData(withRootObject: podcast, requiringSecureCoding: false) UserDefaults.standard.set(data, forKey: favoritePodcastKey) UserDefaults.standard.synchronize() }catch let dataErr { print("@@@ Erro: ", dataErr.localizedDescription) } } @objc fileprivate func handleFetchedSavePodcast() { print("### Fetched") let valueString = UserDefaults.standard.value(forKey: favoritePodcastKey) as? String print("### Value is: ", valueString ?? "") // Retrieve Podcast object from [ Data() that be saved in UserDefaults ] guard let data = UserDefaults.standard.data(forKey: favoritePodcastKey) else { return } guard let podcast = try? NSKeyedUnarchiver.unarchiveTopLevelObjectWithData(data) as? PodCast else { return } print("$$$ ", podcast.trackName ?? "", podcast.artistName ?? "", podcast.artworkUrl600 ?? "") }
el
3 years ago
Thank you fam
petar7
5 years ago
Hi Brian Do you have any idea how to add X(example) image or btn in the cells corner so by executing long press it appears and allows me to delete cell?
HELP & SUPPORT