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
| func removeProduct(product: Product) { | |
| let index = self.products.indexOf { (p : Product) -> Bool in | |
| return product.name == p.name | |
| } | |
| if index != nil { | |
| self.products.removeAtIndex(index!) | |
| } | |
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
| func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool { | |
| return true | |
| } | |
| func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) { | |
| if editingStyle == UITableViewCellEditingStyle.Delete { | |
| if let removedCell = tableView.cellForRowAtIndexPath(indexPath) as? ProductCell { | |
| let removedProduct = Product(name: removedCell.productNameLabel.text!, qty: 1) |
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 ProductManager { | |
| var products : [Product] = [] | |
| var productsKey : String = "products" | |
| let userDefaults = NSUserDefaults.standardUserDefaults() | |
| func saveProductsToDefaults() { | |
| let productsKeyedArchive = NSKeyedArchiver.archivedDataWithRootObject(self.products) |
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 Product : NSObject, NSCoding { | |
| let nameKey = "name" | |
| let qtyKey = "qty" | |
| var name : String = "" | |
| var qty : Int = 0 | |
| required init(coder aDecoder: NSCoder) { | |
| if let name = aDecoder.decodeObjectForKey(nameKey) as? String { |
NewerOlder