0

I am trying to save whatever is in the array arrayOfInt to coreData. The code builds and runs but whenever I try to submit entries the runtime error comes up.

import UIKit
import CoreData

class ViewController: UIViewController {
@IBOutlet var txt: UITextField!
var arrayOfInt = [Int]()


@IBAction func submit(_ sender: Any) {
    if let text = txt.text {
        if let number = Int(text){
            arrayOfInt.append(number)
            let appdeleaget = UIApplication.shared.delegate as! AppDelegate
            let context = appdeleaget.persistentContainer.viewContext
            let NewUser = NSEntityDescription.insertNewObject(forEntityName: "Users", into: context)
            NewUser.setValue(arrayOfInt, forKey: "userName")

            do {
                try context.save()
                print(NewUser)

            }
            catch {
                //
            }

        }}}
}
Hamish
  • 78,605
  • 19
  • 187
  • 280

0 Answers0