My function below when called is not saving whatever is on the textfield to core data. When appearing in the debug area what appears is “”. This code when used in the view did load function it does work but when I call it it does not work.This code saves a image on the imageview perfectly. I dont know why it is working in one area and not in the other.
var playName = UITextField()
func saver() {
let vex = drawPlace.screenshot().pngData()
guard let appDelegate = UIApplication.shared.delegate as? AppDelegate else { return }
let managedContext = appDelegate.persistentContainer.viewContext
let entity = NSEntityDescription.entity(forEntityName: "Users", in: managedContext)!
let item = NSManagedObject(entity: entity, insertInto: managedContext)
let fetch = NSFetchRequest<NSFetchRequestResult>(entityName: "Users")
if let data = vex{
item.setValue(data, forKey: "image")
}
item.setValue(playName.text, forKey: "username")
do {
let result = try? managedContext.fetch(fetch) as? [Users]
print("Queen",result?.count)
try? managedContext.save()
}
catch {
print("Could not save")
}
}
Is playName properly bound? If you set a breakpoint in the method, is there a value to text?
I would keep a guard statement at the top of the method, since it seems like a key part of your code. If it’s coming in empty from GUI, it probably shouldn’t save, right?
You mentioned it works on viewDidLoad, where else are you calling it? Is it from an action? Is the UITextField bound then?
I updated my code above. Less stuff in it but the same problem is happening. I can save my imageview but the string is not saving. The textfield when saved has nothing in it. PlayName is a UITextfield positioned by code in view did load. . This function is a action. Can you show me where I would part the guard part.
I didn’t see a link, but it may not be necessary. Let me explain, the question is: is the UITextField.text property nil or empty? You can check on it with a guard, if it is, then there’s the problem to begin with.
if let textEntered = self.playName.text {
if textEntered.isEmpty != true {
saver(text: textEntered)
print("Saves the textfield text")
} else { print("Text Field was empty nothing to save.")}
} else { print("The text field was nil")
}
playName.resignFirstResponder()
clearAction()
}
func saver(text: String) {
let vex = drawPlace.screenshot().pngData()
guard let appDelegate = UIApplication.shared.delegate as? AppDelegate else { return }
let managedContext = appDelegate.persistentContainer.viewContext
let entity = NSEntityDescription.entity(forEntityName: "Users", in: managedContext)!
let item = NSManagedObject(entity: entity, insertInto: managedContext)
let fetch = NSFetchRequest<NSFetchRequestResult>(entityName: "Users")
if let data = vex{
item.setValue(data, forKey: "image")
print(data)
}
item.setValue(text, forKey: "username")
do {
let result = try? managedContext.fetch(fetch) as? [Users]
print("Queen",result?.count)
try? managedContext.save()
print("Text = \(text)")
}
catch {
print("Could not save")
}
}