Last active
November 19, 2015 03:37
-
-
Save ctrevarthen/57fb040cdc1aadf5fe2b to your computer and use it in GitHub Desktop.
ShopQuick - Favorites Manager Basic
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
| class FavoritesManager { | |
| static let sharedInstance = FavoritesManager() | |
| let userDefaults = NSUserDefaults.standardUserDefaults() | |
| let productsKey = "com.detroitlabs.shopfast.favorites" | |
| private var products : [Product] = [] | |
| init() { | |
| self.loadProductsFromDefaults() | |
| } | |
| func productAtIndex(index: Int) -> Product { | |
| return self.products[index] | |
| } | |
| private func saveProductsToDefaults() { | |
| let productsKeyedArchive = NSKeyedArchiver.archivedDataWithRootObject(self.products) | |
| self.userDefaults.setObject(productsKeyedArchive, forKey: productsKey) | |
| } | |
| private func loadProductsFromDefaults() { | |
| if let data = self.userDefaults.objectForKey(productsKey) as? NSData { | |
| let unarc = NSKeyedUnarchiver(forReadingWithData: data) | |
| if let savedProducts = unarc.decodeObjectForKey("root") as? [Product] { | |
| self.products = savedProducts | |
| } | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment