Skip to content

Instantly share code, notes, and snippets.

View ctrevarthen's full-sized avatar

Chris Trevarthen ctrevarthen

View GitHub Profile
@ctrevarthen
ctrevarthen / gist:ab39ba7cdc848cff54dc
Last active November 19, 2015 03:41
Shopping List Manager - Remove Product
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!)
}
@ctrevarthen
ctrevarthen / gist:9a4151e458eba6d24763
Last active November 19, 2015 03:41
Swipe to delete
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)
@ctrevarthen
ctrevarthen / gist:86470886ec5aeb1650f3
Last active November 19, 2015 03:40
Saving and loading products with NSUserDefaults
class ProductManager {
var products : [Product] = []
var productsKey : String = "products"
let userDefaults = NSUserDefaults.standardUserDefaults()
func saveProductsToDefaults() {
let productsKeyedArchive = NSKeyedArchiver.archivedDataWithRootObject(self.products)
@ctrevarthen
ctrevarthen / gist:f230f5b1603f4c8e97f2
Last active November 19, 2015 03:40
Encoding and Decoding for NSUserDefaults
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 {