Last active
March 12, 2019 14:13
-
-
Save mathonsunday/d8a25d13a882cf075c8856ed0be8293e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import Foundation | |
| import PlaygroundSupport | |
| var userDefaults = UserDefaults(suiteName: #file) | |
| userDefaults!.removePersistentDomain(forName: #file) | |
| let storageNameSpacePrefix = "favorite_events_" | |
| func isFavorited(artistId: String) -> Bool { | |
| return userDefaults!.dictionaryRepresentation().keys.contains(artistIdKey(artistId: artistId)) | |
| } | |
| func isFavoritedStandard(artistId: String) -> Bool { | |
| return UserDefaults.standard.dictionaryRepresentation().keys.contains(artistIdKey(artistId: artistId)) | |
| } | |
| func save(artistId: String) { | |
| let artistIdData = encode(artistId: artistId) | |
| let key = artistIdKey(artistId: artistId) | |
| userDefaults!.set(artistIdData, forKey: key) | |
| } | |
| func remove(artistId: String) { | |
| let key = artistIdKey(artistId: artistId) | |
| userDefaults!.removeObject(forKey: key) | |
| } | |
| private func artistIdKey(artistId: String) -> String { | |
| return "\(storageNameSpacePrefix)\(artistId)" | |
| } | |
| private func encode(artistId: String) -> NSData? { | |
| do { | |
| let data = try NSKeyedArchiver.archivedData(withRootObject: artistId, requiringSecureCoding: true) as NSData | |
| return data | |
| } catch { | |
| NSLog("Error encoding event id") | |
| return nil | |
| } | |
| } | |
| save(artistId: "id") | |
| print(isFavorited(artistId: "id")) | |
| print(isFavoritedStandard(artistId: "id")) | |
| userDefaults!.removePersistentDomain(forName: #file) | |
| print(isFavorited(artistId: "id")) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment