Hey, I am trying to build an app for calories track. I am currently developing the Food class and want to use the CoreData to store it. When I try to add the food I am getting this error ''-[Food setCalories100g:]: unrecognized selector sent to instance 0x600002323430''
Here is part of the code, please somebody help me I am lost.
This is the Food class - Food+CoreDataClass.swift
import Foundation
import CoreData
@objc(Food)
public class Food: NSManagedObject {
}
Food+CoreDataProperties.swift - file
import Foundation
import CoreData
extension Food {
@nonobjc public class func fetchRequest() -> NSFetchRequest<Food> {
return NSFetchRequest<Food>(entityName: "Food")
}
@NSManaged public var name: String?
@NSManaged @objc(calories100g) public var calories100g: Int
@NSManaged public var macroFat: Int
@NSManaged public var macroCarb: Int
@NSManaged public var macroProtein: Int
}
extension Food : Identifiable {
}
and here is the ViewCotroller
// ViewController.swift
// Calories-Tracker
//
// Created by Denislav Todorov on 23.11.22.
//
import UIKit
class FoodsViewController: UIViewController {
@IBOutlet weak var tableView: UITableView!
var foods = [Food]()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
@IBAction func onPlusTapped() {
let alert = UIAlertController(title: "Add Food",
message: nil,
preferredStyle: .alert)
alert.addTextField{ (textField) in
textField.placeholder = "Food Name"
}
alert.addTextField{ (textField) in
textField.placeholder = "Calories per 100g"
textField.keyboardType = .numberPad
}
alert.addTextField{ (textField) in
textField.placeholder = "Fat"
textField.keyboardType = .numberPad
}
alert.addTextField{ (textField) in
textField.placeholder = "Carbohydrate"
textField.keyboardType = .numberPad
}
alert.addTextField{ (textField) in
textField.placeholder = "Protein"
textField.keyboardType = .numberPad
}
let action = UIAlertAction(title: "Add", style: .default) { (_) in
let name = alert.textFields?[0].text
let calories100g = Int((alert.textFields?[1].text)!)
let fat = Int((alert.textFields?[2].text)!)
let carbohydrate = Int((alert.textFields?[3].text)!)
let protein = Int((alert.textFields?[4].text)!)
let food = Food(context: PersistenceService.context)
print(NSStringFromClass(food.classForCoder))
food.name?.append(name!)
//THE ERROR POPS UP and it is for all the properties down
food.calories100g = calories100g ?? 0 //BREAKPOINT
food.macroFat = fat ?? 0
food.macroCarb = carbohydrate ?? 0
food.macroProtein = protein ?? 0
//to here
PersistenceService.saveContext()
self.foods.append(food)
self.tableView.reloadData()
}
alert.addAction(action)
present(alert, animated: true)
}
@IBAction func cancelTapped() {
}
}
extension FoodsViewController: UITableViewDataSource {
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return foods.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell(style: .subtitle, reuseIdentifier: nil)
cell.textLabel?.text = foods[indexPath.row].name
cell.detailTextLabel?.text = String(foods[indexPath.row].calories100g)
cell.detailTextLabel?.text = String(foods[indexPath.row].macroFat)
cell.detailTextLabel?.text = String(foods[indexPath.row].macroCarb)
cell.detailTextLabel?.text = String(foods[indexPath.row].macroProtein)
return cell
}
}
food.calories100g = calories100g ?? 0 //I have my breakpoint on that line and the error is showing here. Thanks in advance!