             return currencies.count
         }
     }
-    
+
     func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
         var cell = self.tableView.dequeueReusableCellWithIdentifier("CurrencyCell")
-    
+
         let index = indexPath.row
         let currency: Currency
         if tableView == self.searchDisplayController!.searchResultsTableView {

            currency = currencies[index]
        }
        let currencyCode = currency.code!
        
        delegate?.didChangeCurrency(currencyCode, targetCurrency: targetCurrency)
        self.dismissViewControllerAnimated(true, completion: {})
    }
    
}

// MARK: - Search

extension ChangeCurrencyViewController: UISearchBarDelegate, UISearchDisplayDelegate {
    
    func filterContentForSearchText(searchText: String) {

        var filteredContent:Array<Currency> = []
        let searchText = searchText.lowercaseString
        
        for currency in currencies {
            let matchesName = currency.name!.lowercaseString.rangeOfString(searchText) != nil
            let matchesCode = currency.code!.lowercaseString.rangeOfString(searchText) != nil
            
            if (matchesName || matchesCode) {
                filteredContent += [currency]
            }
        }
        
        self.searchResults = filteredContent
        
    }
    
    func searchDisplayController(controller: UISearchDisplayController, shouldReloadTableForSearchString searchString: String?) -> Bool {
        self.filterContentForSearchText(searchString!)
        return true
    }
    
}

}

class ChangeCurrencyViewController: UIViewController {

    var managedObjectContext: NSManagedObjectContext!
    var delegate: ChangeCurrencyViewControllerDelegate?
    var targetCurrency: String!
