Post

Replies

Boosts

Views

Activity

Saving and fetching to Core Data
I am trying to save to Core Data so when the App is reopened the data will be there. I have a single ViewController, within that VC I have 10 Text Fields which the user inputs their data (numbers). I have a function where I add all the data for the 7 days (sun - sat) and it shows the total value. I have most in place (xcdatamodeld)... Is it possible to have CoreData fill text fields? Is there a good tutorial on completing core data? Most I have read and watched all use table views for the example.What I want to do is have the same data show up after closing and re-opening the App.   // MARK: - Core Data stack     lazy var persistentContainer: NSPersistentCloudKitContainer = {             let container = NSPersistentCloudKitContainer(name: "Numbers")         container.loadPersistentStores(completionHandler: { (storeDescription, error) in             if let error = error as NSError? {                 // Replace this implementation with code to handle the error appropriately.                 // fatalError() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.                                   /*                  Typical reasons for an error here include:                  * The parent directory does not exist, cannot be created, or disallows writing.                  * The persistent store is not accessible, due to permissions or data protection when the device is locked.                  * The device is out of space.                  * The store could not be migrated to the current model version.                  Check the error message to determine what the actual problem was.                  */                 fatalError("Unresolved error \(error), \(error.userInfo)")             }         })         return container     }()     // MARK: - Core Data Saving support     func saveContext () {         let context = persistentContainer.viewContext         if context.hasChanges {             do {                 try context.save()             } catch {                 // Replace this implementation with code to handle the error appropriately.                 // fatalError() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.                 let nserror = error as NSError                 fatalError("Unresolved error \(nserror), \(nserror.userInfo)")             }         }     }
1
0
735
Feb ’21
Using NSSortDescriptor(key: "date", ascending: true
I have a TableViewController that I am using (datePicker) to add a date to the note with other Attributes. I am using CoreData and fetch to populate another TableViewController with the results. When it populates it shows up with the month: Dec 20, 2021, Feb 1, 2022, Feb 10, 2022, Feb 17, 2022, Feb 2, 2022, Jan 31, 2022, Jan 4, 2022 and so on ... How can I get it to sort correctly? Newest on top Feb 17, 2022 Feb 10, 2022 etc... Thanks in advance. let sortDescriptor = NSSortDescriptor(key: "date", ascending: true) fetchRequest.sortDescriptors = [sortDescriptor]
5
0
2.7k
Feb ’22
UITableViewRowAction Deprecated
I have an app that uses Core Data with iCloud. The delete process works great. Now UITableViewRowAction is Deprecated. My questions are: Do I need to be concerned with that going forward anytime soon? I have implemented UISwipeActionsConfiguration but for I reason I cannot get past after they delete an I reopen the App they re-appear. Are there any write-ups you can point me to that can help me better understand what I am doing wrong? override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {         if editingStyle == .delete {             // delete the row from the data source             inspections.remove(at: indexPath.row)         tableView.deleteRows(at: [indexPath], with: .fade)         }     }      func  tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationsForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {                  // Delete Button          let deleteActions = UIContextualAction(style: .destructive, title: "Delete", handler: { (action, view, success) in              print("Delete")          })          deleteActions.backgroundColor = .red          if let appDelegate = (UIApplication.shared.delegate as? AppDelegate) {                 let context = appDelegate.persistentContainer.viewContext                 let inspectionsToDelete = self.fetchResultController.object(at: indexPath)                 context.delete(inspectionsToDelete)                                  appDelegate.saveContext()             } //         completionHandler(true)                   return UISwipeActionsConfiguration(actions: [deleteActions]) Thanks AndrewG
2
0
946
Mar ’22
How to properly "Delete a row from a tableView" using trailingSwipeActionsConfiguration
I have been stuck on this for some time now, I built a tableView App with the ability to delete a row. I am using with CoreData and iCloud. When I use "editActionsForRowAt" it deletes from the view and does not re-appear when I close and run the App again, the problem is as you know it is deprecated in iOS 13. 1.) This works great but is deprecated in iOS 13 override func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? { //Social Sharing Button let shareAction = UITableViewRowAction(style: UITableViewRowAction.Style.default, title: "share", handler: { (action, indexPath) -> Void in _ = "Just checking in at " + self.inspections[indexPath.row].stateID! }) // Delete Button let deleteAction = UITableViewRowAction(style: UITableViewRowAction.Style.default, title: "Delete",handler: { (action, indexPath) -> Void in if let appDelegate = (UIApplication.shared.delegate as? AppDelegate) { let context = appDelegate.persistentContainer.viewContext let inspectionsToDelete = self.fetchResultController.object(at: indexPath) context.delete(inspectionsToDelete) appDelegate.saveContext() } }) deleteAction.backgroundColor = UIColor(red: 237.0/255.0, green: 66.0/255.0, blue: 106.0/255.0, alpha: 1.0) shareAction.backgroundColor = UIColor(red: 63.0/255.0, green: 212.0/255.0, blue: 78.0/255.0, alpha: 1.0) return [deleteAction, shareAction] } 2.) So I ran with "trailingSwipeActionsConfigurationForRowAt" This should work but it will not swipe to delete the row. func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationsForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? { let deleteAction = UIContextualAction(style: .normal, title: "Delete", handler: {(action, view, success) in if let appDelegate = (UIApplication.shared.delegate as? AppDelegate) { let context = appDelegate.persistentContainer.viewContext let inspectionsToDelete = self.fetchResultController.object(at: indexPath) context.delete(inspectionsToDelete) appDelegate.saveContext() } }) return UISwipeActionsConfiguration(actions: [deleteAction]) } 3.) This deletes without 1.) or 2.) but repopulates the row when I reopen the App override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) { if editingStyle == .delete { // delete the row from the data source inspections.remove(at: indexPath.row) } tableView.deleteRows(at: [indexPath], with: .fade) }
2
0
1.1k
Mar ’22
Edit a row in tableView
Hi Everyone, I built an app that has a detailViewController with a tableView on it. It has a cell and 1 Field Label and 1 Value Label. When I tap to view the detailViewController it will populate what was previously added and saved. I am using CoreData to fetch the data to the detailViewController. My question is how can i make it so if after tapping on a saved job and opening that job in the detailViewController "Editable" there are 21 rows that populate. Ive tried everything I can find in the forums but no luck so far. I managed to be able to delete a row, but cannot edit a row. Below is the code for the delete action. I removed the Edit part of the UISwipeActions. If anyone can help it would be greatly appreciated override func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {      let deleteAction = UIContextualAction(style: .destructive, title: "Delete") {  (action, Sourceview, completionHandler) in              if let appDelegate = (UIApplication.shared.delegate as? AppDelegate) {                                      let context = appDelegate.persistentContainer.viewContext                                      let inspectionsToDelete = self.fetchResultController.object(at: indexPath)                                      context.delete(inspectionsToDelete)                                      appDelegate.saveContext()     }        completionHandler(true)    }        let swipeActions = UISwipeActionsConfiguration(actions: [deleteAction])        return swipeActions }     override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {         if searchController.isActive {         return false         } else {         return true     } }     func deleteData(at indexPath: IndexPath) {          print(indexPath.row) }     func editData(at indexPath: IndexPath) {          print(indexPath.row) }
0
0
571
Apr ’22
Firing a Timer & Resigning the keyboard after a number of characters have been entered(19)
Below is the code I am using to get the timer to fire and the keyboard to resign, THIS WORKS PERFECT ON ANY SIMULATOR. I type in 02272024075900 and it populates 02/27/2024 07:59:00 as soon as I type the last zero the keyboard resigns and the timer fires. But when I run it on my handheld(same device as the simulator, an iPhone 15 Pro). It will not resign the keyboard or fire the timer until I tap one extra digit on the keyboard and then it works perfect. It is not a deal breaker as it is obvious to any user to tap again on the keyboard to have it go away. Why does it work properly on all simulators, but not on my real device. Any thoughts greatly appreciated! func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool { if textField == userInputTextField { if (userInputTextField?.text?.count == 2) || (userInputTextField?.text?.count == 5) { if !(string == "") { userInputTextField?.text = (userInputTextField?.text)! + "/" } } if (userInputTextField?.text?.count == 10) { userInputTextField?.text = (userInputTextField?.text)! + " " } if (userInputTextField?.text?.count == 13) || (userInputTextField?.text?.count == 16) { userInputTextField?.text = (userInputTextField?.text)! + ":" } // Mark check the condition to not exceed 19 chars if let characterCount = userInputTextfield.text?.count { // CHECK FOR CHARACTER COUNT IN TEXT FIELD if characterCount >= 19 { startCountdown((Any).self) // RESIGN FIRST RERSPONDER TO HIDE KEYBOARD return textField.resignFirstResponder() } } } }
2
0
343
Feb ’24