coreDataにdataを保存

setValueして、saveで保存する。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
let appDelegate = UIApplication.shared.delegate as! AppDelegate
        let context = appDelegate.persistentContainer.viewContext
        let entity = NSEntityDescription.entity(forEntityName: "Users", in: context)
        let newUser = NSManagedObject(entity: entity!, insertInto: context)
         
        newUser.setValue("Louis Miguel", forKey: "username")
        newUser.setValue("asdf", forKey: "password")
        newUser.setValue("1", forKey: "age")
         
        do {
            try context.save()
        } catch {
            print("Failed saving")
        }