Bronze and Gold Challenge

#1
//
//  TodoList.swift
//  iTahDoodle
//
//  Created by Arthur de Boer on 08-03-16.
//  Copyright © 2016 Arthur de Boer. All rights reserved.
//

import UIKit

class TodoList: NSObject {
    private let fileURL: NSURL = {
        let documentDirectoryURLs = NSFileManager.defaultManager().URLsForDirectory(.DocumentationDirectory, inDomains: .UserDomainMask)
        let documentDirectoryURL = documentDirectoryURLs.first!
        return documentDirectoryURL.URLByAppendingPathComponent("todolist.items")
    }()
    
    private var items: [String] = []
    
    override init() {
        super.init()
        loadItems()
    }
    
    func saveItems() {
        let itemsArray = items as NSArray
        
        print("Saving items to \(fileURL)")
        if !itemsArray.writeToURL(fileURL, atomically: true) {
            print("Could not save to-do list")
        }
    }
    
    func loadItems() {
        if let itemsArray = NSArray(contentsOfURL: fileURL) as? [String] {
            items = itemsArray
        }
    }
    
    func addItem(item: String) {
        items.append(item)
        saveItems()
    }
    
    func removeItem(index: Int) {
        items.removeAtIndex(index)
        saveItems()
    }
}

extension TodoList: UITableViewDataSource {
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
            return items.count
    }
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath)
        let item = items[indexPath.row]
        
        cell.textLabel!.text = item
        
        return cell
    }
}



//
//  ViewController.swift
//  iTahDoodle
//
//  Created by Arthur de Boer on 08-03-16.
//  Copyright © 2016 Arthur de Boer. All rights reserved.
//

import UIKit

class ViewController: UIViewController, UITableViewDelegate {

    
    
    @IBOutlet var itemTextField: UITextField!
    @IBOutlet var tableView: UITableView!
    
    let todoList = TodoList()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "Cell")
        tableView.dataSource = todoList
        tableView.delegate = self
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    func tableView(tableView: UITableView, willSelectRowAtIndexPath indexPath: NSIndexPath) -> NSIndexPath? {
        todoList.removeItem(indexPath.row)
        tableView.reloadData()
        return indexPath
    }
    
    @IBAction func addButtonPressed(sender: UIButton) {
        guard let text = itemTextField.text where text != "" else {
            return
        }
        todoList.addItem(text)
        itemTextField.text = ""
        tableView.reloadData()
    }

}



#2

This is the Gold Challenge

In ViewController.swift file:


override func viewDidLoad() {
        
...
        tableView.delegate = self
}


extension ViewController: UITableViewDelegate {
    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        todoList.removeItemAtIndex(indexPath.row)
        tableView.reloadData()
    }
}

In TodoList.swift


func removeItemAtIndex(index: Int) {
        guard items.indices.contains(index) else {
            return
        }
        items.removeAtIndex(index)
        saveItems()
}
#3

I modified your code a bit, and moved everything to TodoList.swift from ViewControler.swift. Still using your

func removeItemAtIndex(index: Int) {
        guard items.indices.contains(index) else {
            return
        }
        items.removeAtIndex(index)
        saveItems()
}

and added this to the extension

extension TodoList : UITableViewDataSource {
    ...
    func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
            if editingStyle == UITableViewCellEditingStyle.Delete {
                removeItem(indexPath.row)
                tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Automatic)
                saveItems()
            }
    }
}