# Function not saving string on textfield to core data

**URL:** <https://forums.kodeco.com/t/function-not-saving-string-on-textfield-to-core-data/78400>\
**Category:** Swift\
**Created:** [August 6, 2019, 10:23pm UTC](https://forums.kodeco.com/t/function-not-saving-string-on-textfield-to-core-data/78400 "2019-08-06T22:23:51Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![timswift](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/timswift/32/110937_2.png) [@timswift](https://forums.kodeco.com/u/timswift)\
**Post date:** [August 6, 2019, 10:23pm UTC](https://forums.kodeco.com/t/function-not-saving-string-on-textfield-to-core-data/78400/1 "2019-08-06T22:23:52Z")

</div>

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")
    }
    
}

```

---

<div class="post-metadata">

**Author:** ![robertomachorro](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/robertomachorro/32/111603_2.png) [@robertomachorro](https://forums.kodeco.com/u/robertomachorro)\
**Post date:** [August 6, 2019, 11:04pm UTC](https://forums.kodeco.com/t/function-not-saving-string-on-textfield-to-core-data/78400/3 "2019-08-06T23:04:09Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![timswift](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/timswift/32/110937_2.png) [@timswift](https://forums.kodeco.com/u/timswift)\
**Post date:** [August 7, 2019, 3:19am UTC](https://forums.kodeco.com/t/function-not-saving-string-on-textfield-to-core-data/78400/4 "2019-08-07T03:19:44Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![timswift](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/timswift/32/110937_2.png) [@timswift](https://forums.kodeco.com/u/timswift)\
**Post date:** [August 7, 2019, 3:29am UTC](https://forums.kodeco.com/t/function-not-saving-string-on-textfield-to-core-data/78400/5 "2019-08-07T03:29:55Z")

</div>

This is a link to my project. Code is just not saving the text. Thanks.

---

<div class="post-metadata">

**Author:** ![robertomachorro](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/robertomachorro/32/111603_2.png) [@robertomachorro](https://forums.kodeco.com/u/robertomachorro)\
**Post date:** [August 7, 2019, 11:54am UTC](https://forums.kodeco.com/t/function-not-saving-string-on-textfield-to-core-data/78400/6 "2019-08-07T11:54:28Z")

</div>

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.

```
@IBAction func buttonPressed(_ sender: Any) {
    guard let text = playName.text, text.count > 0 else { return }
    print("Text = \(text)")
}

```

Another quick way to do it, is to setup a breakpoint on the save and inspect the value.

---

<div class="post-metadata">

**Author:** ![timswift](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/timswift/32/110937_2.png) [@timswift](https://forums.kodeco.com/u/timswift)\
**Post date:** [August 7, 2019, 12:28pm UTC](https://forums.kodeco.com/t/function-not-saving-string-on-textfield-to-core-data/78400/7 "2019-08-07T12:28:41Z")

</div>

Just added that guard code seems to have no effect. This is the link to my project [Dropbox - CoreDataDemo-master.zip - Simplify your life](https://www.dropbox.com/s/437zr34a223gwhm/CoreDataDemo-master.zip?dl=0).

---

<div class="post-metadata">

**Author:** ![robertomachorro](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/robertomachorro/32/111603_2.png) [@robertomachorro](https://forums.kodeco.com/u/robertomachorro)\
**Post date:** [August 8, 2019, 1:07am UTC](https://forums.kodeco.com/t/function-not-saving-string-on-textfield-to-core-data/78400/8 "2019-08-08T01:07:01Z")

</div>

When setting the breakpoint, what did you see for _playName.text_?

---

<div class="post-metadata">

**Author:** ![timswift](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/timswift/32/110937_2.png) [@timswift](https://forums.kodeco.com/u/timswift)\
**Post date:** [August 8, 2019, 3:21am UTC](https://forums.kodeco.com/t/function-not-saving-string-on-textfield-to-core-data/78400/9 "2019-08-08T03:21:33Z")

</div>

this is a photo of my debugg area. ![56%20PM](https://files.chunter.kodeco.com/original/3X/5/3/535135fb0c9b6a0ca2b18f68a1e900cb1f33c040.png)

---

<div class="post-metadata">

**Author:** ![robertomachorro](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/robertomachorro/32/111603_2.png) [@robertomachorro](https://forums.kodeco.com/u/robertomachorro)\
**Post date:** [August 8, 2019, 10:41am UTC](https://forums.kodeco.com/t/function-not-saving-string-on-textfield-to-core-data/78400/10 "2019-08-08T10:41:16Z")

</div>

What happens if you step over (advance the instruction pointer)? Does it return on the method or proceed?

---

<div class="post-metadata">

**Author:** ![fiavvek](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/fiavvek/32/81494_2.png) [@fiavvek](https://forums.kodeco.com/u/fiavvek)\
**Post date:** [August 9, 2019, 4:46am UTC](https://forums.kodeco.com/t/function-not-saving-string-on-textfield-to-core-data/78400/11 "2019-08-09T04:46:00Z")

</div>

in submitAction(), you start off by setting playName.text = “”. This then breaks at the guard point since the length is 0.

---

<div class="post-metadata">

**Author:** ![timswift](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/timswift/32/110937_2.png) [@timswift](https://forums.kodeco.com/u/timswift)\
**Post date:** [August 9, 2019, 1:25pm UTC](https://forums.kodeco.com/t/function-not-saving-string-on-textfield-to-core-data/78400/12 "2019-08-09T13:25:18Z")

</div>

This is the photo of what I think you are asking me to do. ![41%20AM](https://files.chunter.kodeco.com/original/3X/f/e/fe596314614566ed1e23b6dad0defd23a3551fc2.png) This is while I run the app with the breakpoint.

---

<div class="post-metadata">

**Author:** ![robertomachorro](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/robertomachorro/32/111603_2.png) [@robertomachorro](https://forums.kodeco.com/u/robertomachorro)\
**Post date:** [August 9, 2019, 5:42pm UTC](https://forums.kodeco.com/t/function-not-saving-string-on-textfield-to-core-data/78400/13 "2019-08-09T17:42:39Z")

</div>

That’s exactly it! Except none of the local constants or variables are showing. I’ll download the source over the weekend and will take a look.

_Side note:_ Github is best for sharing code, I can upload/commit my fixes and you can track them with annotations.

---

<div class="post-metadata">

**Author:** ![stromyc](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/stromyc/32/81494_2.png) [@stromyc](https://forums.kodeco.com/u/stromyc)\
**Post date:** [August 9, 2019, 9:23pm UTC](https://forums.kodeco.com/t/function-not-saving-string-on-textfield-to-core-data/78400/14 "2019-08-09T21:23:34Z")

</div>

I made these changes to your code and it worked.

@objc func submitAction() {

```
    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")
    }
    
}

```

}

---

<div class="post-metadata">

**Author:** ![timswift](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/timswift/32/110937_2.png) [@timswift](https://forums.kodeco.com/u/timswift)\
**Post date:** [August 10, 2019, 3:49am UTC](https://forums.kodeco.com/t/function-not-saving-string-on-textfield-to-core-data/78400/15 "2019-08-10T03:49:02Z")

</div>

Thats It it worked. Your the man or woman whatever your sex is!

---

<div class="post-metadata">

**Author:** ![developerxyz](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/developerxyz/32/81494_2.png) [@developerxyz](https://forums.kodeco.com/u/developerxyz)\
**Post date:** [August 11, 2019, 7:49am UTC](https://forums.kodeco.com/t/function-not-saving-string-on-textfield-to-core-data/78400/16 "2019-08-11T07:49:39Z")

</div>

Unwrap the `playerName`.text as `playName.text!` when setting the value

---

<div class="post-metadata">

**Author:** ![system](https://s3.amazonaws.com/cdn.raywenderlich.com/community/forum-assets/noun-game-ghost-878668.png) [@system](https://forums.kodeco.com/u/system)\
**Post date:** [January 20, 2020, 2:23pm UTC](https://forums.kodeco.com/t/function-not-saving-string-on-textfield-to-core-data/78400/17 "2020-01-20T14:23:53Z")

</div>

Closed on January 20, 2020, 2:23pm UTC

This topic was automatically closed after 166 days. New replies are no longer allowed.
