UiKit Basics: Guding Resources for "Now For Real Challenge" - Apple Developer Academy IFCE
func saveContext () {
let context = persistentContainer.viewContext
if context.hasChanges {
do {
try context.save()
} catch {
let nserror = error as NSError
fatalError("Unresolved error \(nserror), \(nserror.userInfo)")
}
}
}
func applicationWillTerminate(_ application: UIApplication) {
self.saveContext()
}
lazy var persistentContainer: NSPersistentContainer = {
let container = NSPersistentContainer(name: "DataModel")
container.loadPersistentStores(completionHandler: { (storeDescription, error) in
if let error = error as NSError? {
fatalError("Unresolved error \(error), \(error.userInfo)")
}
})
return container
}()
let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
func saveItems() {
do {
try context.save()
} catch {
print("Error saving context \(error)")
}
}
func loadItems(with request: NSFetchRequest<Item> = Item.fetchRequest(), predicate: NSPredicate? = nil) {
let categoryPredicate = NSPredicate(format: "parentCategory.name MATCHES %@", selectedCategory!.name!) //QUERY
if let addtionalPredicate = predicate {
request.predicate = NSCompoundPredicate(andPredicateWithSubpredicates: [categoryPredicate, addtionalPredicate])
} else {
request.predicate = categoryPredicate
}
do {
ARRAY_OF_OBJECTS = try context.fetch(request)
} catch {
print("Error fetching data from context \(error)")
}
}
itemArray[0].done = true
saveItems()
let request : NSFetchRequest<Item> = Item.fetchRequest()
let predicate = NSPredicate(format: "title CONTAINS[cd] %@", INPUT_@)
request.sortDescriptors = [NSSortDescriptor(key: "title", ascending: true)
loadItems(with: request, predicate: predicate)
context.delete(itemArray[0]) //Temporary
itemArray.remove(at: 0)
saveItems()
var textField = UITextField()
let alert = UIAlertController(title: "Add New Todoey Item", message: "", preferredStyle: .alert)
let action = UIAlertAction(title: "Add Item", style: .default) { (action) in
}
alert.addTextField { (alertTextField) in
alertTextField.placeholder = "Create new Item"
textField = alertTextField
}
alert.addAction(action)
present(alert, animated: true,completion: nil)
@IBAction func addButtonPressed(_ sender: UIBarButtonItem) {}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
//print(itemArray[indexPath.row])
if tableView.getAccessoryType(indexPath: indexPath) == .checkmark{
tableView.setAccessoryType(indexPath: indexPath, accessoryType: .none)
}else{
tableView.setAccessoryType(indexPath: indexPath, accessoryType: .checkmark)
}
tableView.deselectRow(at: indexPath, animated: true)
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 3 //create 3 cells
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Identifier", for: indexPath)
cell.textLabel?.text = "titulo"
return cell
}