Skip to content

Instantly share code, notes, and snippets.

@NunoAlexandre
Last active June 2, 2023 10:05
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save NunoAlexandre/8bd8709ffa3b2f7c8759dfe79bb6702a to your computer and use it in GitHub Desktop.
Save NunoAlexandre/8bd8709ffa3b2f7c8759dfe79bb6702a to your computer and use it in GitHub Desktop.
Extend 'KeychainWrapper' to store and lookup Dictionaries
import Foundation
import SwiftKeychainWrapper
let keychain: KeychainWrapper = KeychainWrapper.standard
/// Add methods that allow a client to save and lookup Dict's.
extension KeychainWrapper {
func saveDict(_ value: [String: Any], forKey key: String) {
self.set(NSKeyedArchiver.archivedData(withRootObject: value),
forKey: key)
}
func dict(forKey key: String) -> [String: Any]? {
if let storedData = self.data(forKey: key) {
return NSKeyedUnarchiver.unarchiveObject(with: storedData) as? [String: Any]
}
return nil
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment