Для вставки значения в основные данные я поделюсь примером, вы можете установить в соответствии с вашими данными.
Шаг 1: - Задайте объект следующим образом
Шаг 2: -
//MARK:- FOR CREATING NEW DATA
func createData(){
//As we know that container is set up in the AppDelegates so we need to refer that container.
guard let appDelegate = UIApplication.shared.delegate as? AppDelegate else { return }
//We need to create a context from this container
let managedContext = appDelegate.persistentContainer.viewContext
//Now let’s create an entity and new user records.
let userEntity = NSEntityDescription.entity(forEntityName: "User", in: managedContext)!
//final, we need to add some data to our newly created record for each keys using
let user = NSManagedObject(entity: userEntity, insertInto: managedContext)
user.setValue(txtFldUsername.text!, forKeyPath: "username")
user.setValue(txtFldEmail.text!, forKey: "email")
user.setValue(txtFldPassword.text!, forKey: "password")
retrieveData()
//Now we have set all the values. The next step is to save them inside the Core Data
do {
try managedContext.save()
} catch let error as NSError {
print("Could not save. \(error), \(error.userInfo)")
}
}
Stop 3: - Для выборки данных
//MARK:- FOR RETRIEVING OR FETCHING THE DATA
func retrieveData() {
//As we know that container is set up in the AppDelegates so we need to refer that container.
guard let appDelegate = UIApplication.shared.delegate as? AppDelegate else { return }
//We need to create a context from this container
let managedContext = appDelegate.persistentContainer.viewContext
//Prepare the request of type NSFetchRequest for the entity
let fetchRequest = NSFetchRequest<NSFetchRequestResult>(entityName: "User")
do {
let result = try managedContext.fetch(fetchRequest)
arrUserDetails.removeAll()
for data in result as! [NSManagedObject] {
print(data.value(forKey: "username") as! String)
arrUserDetails.append(userData(username: (data.value(forKey: "username") as! String), email: (data.value(forKey: "email") as! String), password: (data.value(forKey: "password") as! String)))
}
tblView.reloadData()
} catch {
print("Failed")
}
}
Все еще есть проблема, дайте мне знать о запросе