Andreas Sjöstedt
Andreas Sjöstedt

Reputation: 107

How can I get an array to an NSManagedObject

I am sorry if I put out silly questions but I am new to Swift. I am building an app and so far it goes quite well. But now I want to delete some rows from my tableview which gets feeded from an Array with Strings. And then I want to save/fetch that using core data. I believe I have the code for it but the problem is that I am trying to save an array full of Strings. so I get error message saying: Cannot convert value of type 'String' to expected argument type 'NSManagedObject'. And therefore I am wondering, how can I solve this? Is there any way to "add those Strings to an NSManagedObject somehow?

Here are my code:

the array : enter image description here

and here are the code:

import UIKit
import CoreData




class tableViewController: UIViewController, UITableViewDelegate, UITableViewDataSource{
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return (List.count)

}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) ->     UITableViewCell {
    let cell = UITableViewCell(style: .default, reuseIdentifier: "cell")
    cell.textLabel?.text = List.self[indexPath.row]
    cell.textLabel?.textColor = UIColor.white
    cell.backgroundColor = UIColor.clear




    return(cell)

}

    func tableView(_ tableView: UITableView, commit editingStyle:     UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {

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

        if editingStyle == .delete{
            let rows = List[indexPath.row]
            context.delete(rows)
            (UIApplication.shared.delegate as! AppDelegate).saveContext()

            do{
                List = try context.fetch(MealsMenu.fetchRequest()) as! [String]
            }
            catch{
                print(error)
            }
        }
        myTableView.reloadData()

}





@IBOutlet weak var myTableView: UITableView!

override func viewDidAppear(_ animated: Bool) {
    myTableView.reloadData()
}

override func viewDidLoad() {
    super.viewDidLoad()
    CoredataClass.saveItems()
    CoredataClass.loadData()
}

}

Upvotes: 1

Views: 214

Answers (1)

Alejandro L.Rocha
Alejandro L.Rocha

Reputation: 1145

You will need to create a function like this

Imagine that your Entity is Recipe which has an string attribute recipeName which contains the name of the recipe.

func fetchPersistentData() -> [String] {


    var recipes = [String]()

    guard let appDelegate = UIApplication.shared.delegate as? AppDelegate else {

        return recipes
    }

    let managedContext = appDelegate.persistentContainer.viewContext

    let fetchRequest = NSFetchRequest<Recipe>(entityName: "Recipe")

    do {
        let fetchedResults = try managedContext.fetch(fetchRequest)

        if fetchedResults.count > 0 {

            recipes = fetchedResults.map { ("\($0.recipeName)")}

            return recipes
        }

    } catch let error as NSError {
        // Wrong handling
        print(error.description)
    }

    return recipes
}

Upvotes: 4

Related Questions