Giter Site home page Giter Site logo

uikitbasics's Introduction

UiKitBasics

UiKit Basics: Guding Resources for "Now For Real Challenge" - Apple Developer Academy IFCE

Core Data

1 - Create a Data Model File

2 - On App Delegate

Core Data Saving Support

 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()
    }

Core Data stack

 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
    }()

CRUD

    let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext

Save Item

 func saveItems() {
        
        do {
          try context.save()
        } catch {
           print("Error saving context \(error)")
        }
        
    }
 

Load Item

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)")
       }
       
       
   }

Update

  itemArray[0].done = true
  saveItems()

Select

  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)

Delete

context.delete(itemArray[0]) //Temporary
itemArray.remove(at: 0)
saveItems()

Alert

  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)       

Buttons

@IBAction func addButtonPressed(_ sender: UIBarButtonItem) {}

TableView

Configure

TableView Delegate

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)
        
    }

TableView DataSource Methods

    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
    }

uikitbasics's People

Contributors

joaoipiraja avatar

Stargazers

 avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.