Posts

Post not yet marked as solved
5 Replies
161 Views
I created a core data data model and now I want to total/sum one of the attributes. The data model is as follows: import CoreData @objc(Aktie) class Aktie: NSManagedObject {     @NSManaged var deletedDate: Date?     @NSManaged var id: NSNumber!     @NSManaged var title: String?     @NSManaged var point: String? } The code for the ViewController to show the total is: import UIKit import CoreData var points = [Aktie]() var firstLoad = true class TotalPointsViewController: UIViewController {     @IBOutlet weak var totalPoints: UILabel!       override func viewDidLoad() {         super.viewDidLoad()         if(firstLoad)     {             firstLoad = false             let appDelegate = UIApplication.shared.delegate as! AppDelegate             let context: NSManagedObjectContext = appDelegate.persistentContainer.viewContext             let request = NSFetchRequest<NSFetchRequestResult>(entityName: "Aktie")             do {                 let results: NSArray = try context.fetch(request) as NSArray                 for result in results {                     let aktie = result as! Aktie                     points.append(aktie)                 }             }             catch             {                 print("Fetch Failed")             }         }     }     // Mark: Add total of points func calculateSumPoints() -> [Aktie] {     let sum = 0     for punt in points.point {         sum += Int(punt)      }     totalPoints.text = "\(sum)"     } } What am I doing wrong?
Posted
by NickWols.
Last updated
.
Post marked as solved
3 Replies
121 Views
I implemented core data and UISteppers in some fields. Every time I am trying to edit a record the UIStepper start counting from 1. import CoreData class AktieViewController: UIViewController {         @IBOutlet weak var titleTF: UITextField!     @IBOutlet weak var typeTF: UITextField!     @IBOutlet weak var notesTV: UITextView!     @IBOutlet weak var counterTF: UITextField!     @IBOutlet weak var pointTF: UITextField!     @IBOutlet weak var savingsTF: UITextField!     @IBOutlet weak var completedTF: UITextField!     @IBOutlet weak var counterStepper: UIStepper!     @IBOutlet weak var pointsStepper: UIStepper!     @IBOutlet weak var savingsStepper: UIStepper!               var selectedAktie: Aktie? = nil          override func viewDidLoad()     {         super.viewDidLoad()         if(selectedAktie != nil)         {             titleTF.text = selectedAktie?.title             typeTF.text = selectedAktie?.type             notesTV.text = selectedAktie?.notes             savingsTF.text = selectedAktie?.saving             counterTF.text = selectedAktie?.counter             pointTF.text = selectedAktie?.point             completedTF.text = selectedAktie?.completed         }     }         // Mark: this is to save the action          @IBAction func saveAction(_ sender: Any) {                       let appDelegate = UIApplication.shared.delegate as! AppDelegate         let context: NSManagedObjectContext = appDelegate.persistentContainer.viewContext         if(selectedAktie == nil)         {             let entity = NSEntityDescription.entity(forEntityName: "Aktie", in: context)             let newAktie = Aktie (entity: entity!, insertInto: context)             newAktie.id = aktieList.count as NSNumber             newAktie.title = titleTF.text             newAktie.type = typeTF.text             newAktie.notes = notesTV.text             newAktie.saving = savingsTF.text             newAktie.point = pointTF.text             newAktie.counter = counterTF.text             newAktie.completed = completedTF.text             do {                 try context.save()                 aktieList.append(newAktie)                 navigationController?.popViewController(animated: true)         }         catch         {             print("context save error")         }     }         else // edit         {             let request = NSFetchRequest<NSFetchRequestResult>(entityName: "Aktie")             do {                 let results: NSArray = try context.fetch(request) as NSArray                 for result in results {                     let aktie = result as! Aktie                     if(aktie == selectedAktie)                     {                         aktie.title = titleTF.text                         aktie.type = typeTF.text                         aktie.notes = notesTV.text                         aktie.saving = savingsTF.text                         aktie.point = pointTF.text                         aktie.counter = counterTF.text                         aktie.completed = completedTF.text                                                                           try context.save()                         navigationController?.popViewController(animated: true)                     }                 }             }             catch             {                 print("Fetch Failed")             }         }     }          // this is to delete the action                   @IBAction func deletedAktie(_ sender: Any) {              let appDelegate = UIApplication.shared.delegate as! AppDelegate         let context: NSManagedObjectContext = appDelegate.persistentContainer.viewContext                  let request = NSFetchRequest<NSFetchRequestResult>(entityName: "Aktie")         do {             let results: NSArray = try context.fetch(request) as NSArray             for result in results {                 let aktie = result as! Aktie                 if(aktie == selectedAktie)                 {                     aktie.deletedDate = Date ()                     try context.save()                     navigationController?.popViewController(animated: true)                 }             }         }         catch         {             print("Fetch Failed")         }              }     // This function adds the stepper to a field         //issue: it does not remember the score when i edit it and starts over          @IBAction func counterStepperPressed(_ sender: UIStepper) {         counterTF.text = Int(sender.value).description     }              @IBAction func pointStepperPressed(_ sender: UIStepper) {         pointTF.text = Int(sender.value).description     }              @IBAction func savingsStepperPressed(_ sender: UIStepper) {         savingsTF.text = Int(sender.value).description     }      }
Posted
by NickWols.
Last updated
.
Post not yet marked as solved
0 Replies
86 Views
I am trying to use the following data model to create a table view. I have 3 questions? where can I find more detailed information to set up table views? how do I set up my section statement in the case I want to use a dictionary/array? (actionGoal) in 'numberOfsections' and 'titleForHeaderinSection' how do I set up an int and an dictionary/array in 'cellForRowAt'. This is the data model. import Foundation struct ActionResult: Codable {     let data: [ActionElement] } struct ActionElement: Codable {     let actionID: Int     let actionItem: String     let actionGoal: ActionGoal     let actionImage: String     let actionBenefit: ActionBenefit     let actionSavings: Int     let actionType: ActionType     let actionDescription, actionTips: String     let actionInformationURL: String     let actionSponsorURL: String     enum CodingKeys: String, CodingKey {         case actionID = "ActionID"         case actionItem = "ActionItem"         case actionGoal = "ActionGoal"         case actionImage = "ActionImage"         case actionBenefit = "ActionBenefit"         case actionSavings = "ActionSavings"         case actionType = "ActionType"         case actionDescription = "ActionDescription"         case actionTips = "ActionTips"         case actionInformationURL = "ActionInformationURL"         case actionSponsorURL = "ActionSponsorURL"     } } enum ActionBenefit: String, Codable {     case costs = "Costs"     case education = "Education"     case environment = "Environment"     case health = "Health" } enum ActionGoal: String, Codable {     case cleanEnergy = "Clean Energy"     case cleanWater = "Clean Water"     case climateAction = "Climate Action"     case economicGrowth = "Economic Growth"     case goodHealth = "Good Health"     case noPoverty = "No *******"     case promoteEquality = "Promote Equality"     case qualityEducation = "Quality Education"     case responsibleConsumption = "Responsible Consumption"     case zeroHunger = "Zero Hunger" } enum ActionType: String, Codable {     case sticky = "Sticky"     case todoe = "Todoe" } typealias Action = [ActionElement] This is the code for the sections:         return result?.data.count ?? 0                        }     override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {         return result?.data[section].actionGoal        } Error: Type of expression is ambiguous without more context. And this is the 'cellForRowAt' code:                 cell.actionBenefit.text = action?.actionBenefit                 cell.actionSavings.text = action?.actionSavings error codes: Cannot assign value of type 'ActionType?' to type 'String?' Cannot assign value of type 'Int?' to type 'String?' I know I have asked this type of question before but I am trying to understand the concept. Please support me.
Posted
by NickWols.
Last updated
.
Post not yet marked as solved
0 Replies
118 Views
I am trying to create a note pad in Xcode with core data but I am hitting a wall when I create the table view. The error is at the code:   noteCell.titleLabel.text = thisNote.title The error is :Value of type 'UITableView' has no member 'text' var noteList = [Note]() class NoteTableView: UITableViewController {          override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell          {              var noteCell = tableView.dequeueReusableCell(withIdentifier: "noteCellID", for: indexPath) as! NoteCell                     let thisNote: Note!         thisNote = noteList[indexPath.row]                  noteCell.titleLabel.text = thisNote.title         noteCell.descriptionLabel.text = thisNote.desc                      return noteCell                      }          override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int     {         return noteList.count          }         override func viewDidAppear(_ animated: Bool) {         tableView.reloadData()              }      } The noteCell is defined as follows. class NoteCell: UITableViewCell {   @IBOutlet var titleLabel: UITableView! @IBOutlet var descriptionLabel: UITableView!      } The core date is defined as follows: import CoreData @objc(Note) class Note: NSManagedObject {     @NSManaged var id: NSNumber!     @NSManaged var title: String?     @NSManaged var desc: String?     @NSManaged var deletedDate: Date? } Thank you!
Posted
by NickWols.
Last updated
.
Post not yet marked as solved
1 Replies
168 Views
I have data with a section. I followed the instructions to add a search bar but I am running into errors due to my sections. My data model looks like this: struct ActionResult: Codable {     let data3: [Datum] } struct Datum: Codable {     let actionGoal, actionGoalDescription, actionGoalImage: String     let actions: [Action] } struct Action: Codable {     let actionTitle: String     let actionID: Int     let select, completed, favorite: Bool     let actionType, actionDescription, actionTips, actionImage: String     let actionSponsor, actionSponsorURL: String My table view code looks like this: import SwiftUI class ActionViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, UISearchBarDelegate {          @IBOutlet weak var ActionTableView: UITableView!     @IBOutlet weak var SearchBar: UISearchBar!          var result: ActionResult?     var index = 0     var filteredData: [String]?          private let tableView: UITableView = {         let table = UITableView(frame: .zero,                                 style: .grouped)         table.register(UITableViewCell.self, forCellReuseIdentifier: "ActionCell")         return table     }()          override func viewDidLoad() {         super.viewDidLoad()         parseJSON()         view.addSubview(tableView)         self.tableView.frame = view.bounds         self.tableView.delegate = self         self.tableView.dataSource = self         SearchBar.delegate = self         filteredData = result?.data3.actions The last line has the error code: Value of type [Datum] has no member actions. The code for the search bar looks like this:              func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {                      filteredData = []                          if searchText == "" {                 filteredData = result?.data3.actions                              }             else {                 for action in result?.data3.actions {                 if action.lowercase().contains(searchText.lowercased()) {                     filteredData.append(action)                 }             }         }             self.tableView.reloadData()     } And this code has the same errors. Value of type [Datum] has no member actions. I need help how to declare that my data model has sections. Thanks!
Posted
by NickWols.
Last updated
.
Post marked as solved
5 Replies
349 Views
I have parsed a json file with the following structure into a table view succesfully. import Foundation struct ActionResult: Codable {     let data3: [Datum] } struct Datum: Codable {     let actionGoal, actionGoalDescription, actionGoalImage: String     let actions: [Action] } struct Action: Codable {     let actionTitle: String     let actionID: Int     let select, completed, favorite: Bool     let actionType, actionDescription, actionTips, actionImage: String     let actionSponsor, actionSponsorURL: String Now I am preparing a segue to a detail ViewController but is giving me an error.  func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {             performSegue(withIdentifier: "showDetail", sender: self)        }         override func prepare(for segue: UIStoryboardSegue, sender: Any?) {             if let destination = segue.destination as? ActionDetailViewController {                 destination.action = result?.data3.actions[(tableView.indexPathForSelectedRow?.row)!] Error description. Value of type '[Datum]' has no member 'actions' Who can help me?
Posted
by NickWols.
Last updated
.
Post marked as solved
2 Replies
183 Views
I have parsed a json file with the following structure into a table view succesfully. import Foundation struct ActionResult: Codable {     let data3: [Datum] } struct Datum: Codable {     let actionGoal, actionGoalDescription, actionGoalImage: String     let actions: [Action] } struct Action: Codable {     let actionTitle: String     let actionID: Int     let select, completed, favorite: Bool     let actionType, actionDescription, actionTips, actionImage: String     let actionSponsor, actionSponsorURL: String Now I am preparing a segue to a detail ViewController but is giving me an error.      func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {             performSegue(withIdentifier: "showDetail", sender: self)        }         override func prepare(for segue: UIStoryboardSegue, sender: Any?) {             if let destination = segue.destination as? ActionDetailViewController {                 destination.action = result?.data3.actions[(tableView.indexPathForSelectedRow?.row)!] Error description. Value of type '[Datum]' has no member 'actions' Who can help me? Additional info: DetailViewController set up import UIKit import SwiftUI class ActionDetailViewController: UIViewController {     @IBOutlet weak var ActionImageDetail: UIImageView!     @IBOutlet weak var ActionTitleDetail: UILabel!     @IBOutlet weak var ActionDescriptionDetail: UITextView!     @IBOutlet weak var ActionTipsDetail: UITextView!          var action: Action?          override func viewDidLoad() {         super.viewDidLoad()         ActionTitleDetail.text = action?.actionTitle         ActionImageDetail.image = UIImage(named: action!.actionImage)         ActionDescriptionDetail.text = action?.actionDescription         ActionTipsDetail.text = action?.actionTips                         }         override func didReceiveMemoryWarning() {         super.didReceiveMemoryWarning()     } }
Posted
by NickWols.
Last updated
.
Post not yet marked as solved
2 Replies
143 Views
I am stuck with the following code: I am trying to build a segue between a table view and a detail view with an attached json file error: Thread 1: Fatal error: Unexpectedly found nil while implicitly unwrapping an Optional value What am I doing wrong? Code:     // This code is for the transfer to the next page             func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {             let vc = storyboard?.instantiateViewController(withIdentifier: "ActionDetailViewController") as? ActionDetailViewController             let action = result?.data3[indexPath.row]             vc?.ActionTitleDetail.text = action?.actionTitle               self.navigationController?.pushViewController(vc!, animated: true) Also I need help to use more details from the json file on the detail view besides the elements in the table view. Thanks
Posted
by NickWols.
Last updated
.