我正在尝试将数组arrayOfInt中的任何内容保存到coreData。代码构建并运行,但每当我尝试提交条目时,都会出现运行时错误。
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 {
//
}
}}}
}