when I add an edit data in the Core data then add another nil cell on the Table

before write down what I want to say
my English is not good but plz give me an answer
my situation is when I edit my text and save it it works well but the nil cell is added on the table at the same time
when I add a new text then it never make a nil cell but only edit works wrong plz give me any advices or answer

here is my code

import UIKit

import CoreData

class ComposeViewController: UIViewController {



    

    

    

    let formatter:DateFormatter = {

       let formatter = DateFormatter()

        formatter.dateFormat = "YYYY년 MM월 dd일 E요일"

        formatter.locale = Locale.init(identifier: "koKR")

       return formatter

    }()



   

//    var segueData: TodoList?

    var editSegueData: TodoList?

    var itemContent:[NSManagedObject] = []

    

    @IBOutlet weak var dateLabel: UILabel!

    @IBOutlet weak var textView: UITextView!

    

    @IBAction func save(
sender: AnyObject) {

        guard let memo = textView.text , memo.count > 0 else{

            alert(message: "텍스트를 입력하세요")

            return

        }

        

        let date = Date()

        let appDelegate = UIApplication.shared.delegate as! AppDelegate

        let context = appDelegate.persistentContainer.viewContext

        let entity = NSEntityDescription.entity(forEntityName: "TodoList", in: context)!

        let theTitle = NSManagedObject(entity: entity, insertInto: context)

        let checkBoxBool = false



        

        if let target = editSegueData

        {

            print("target.content = \(target.content) \(target.insertDate)")

            target.content = memo

            do

            {

                try context.save()

                

//                itemContent.append(theTitle)

            }

            catch

            {

                print("There was a error in saving")

            }

            NotificationCenter.default.post(name:ComposeViewController.memoDidChange, object: nil)

        }

        else

        {

            theTitle.setValue(memo, forKey: "content")

            theTitle.setValue(date, forKey: "insertDate")

            theTitle.setValue(checkBoxBool, forKey: "checkBox")

            do

            {

                try context.save()

//                itemContent.append(theTitle)

            }

            catch

            {

                print("There was a error in saving")

            }

        }

        NotificationCenter.default.post(name: ComposeViewController.newMemoDidInsert, object: nil)

        



        dismiss(animated: true, completion: nil)

    }

    

    @IBAction func cancelButton(_ sender: Any) {

        dismiss(animated: true, completion: nil)

    }

    

    

    override func viewDidLoad() {

        super.viewDidLoad()

        dateLabel.text = formatter.string(from: Date())

        if let memo = editSegueData {

            navigationItem.title = "EDIT"

            textView.text = memo.content

        }else{

            navigationItem.title = "NEW"

            textView.text = ""

        }



        // Do any additional setup after loading the view.

    }

    



    /*

    // MARK: - Navigation



    // In a storyboard-based application, you will often want to do a little preparation before navigation

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

        // Get the new view controller using segue.destination.

        // Pass the selected object to the new view controller.

    }

    */



}



extension ComposeViewController {

    static let newMemoDidInsert = Notification.Name("newMemoDidInsert")

    static let memoDidChange = Notification.Name(rawValue: "memoDidChange")

}
Answered by Claude31 in 628211022

line 45 works wrong 
editing is working well but when I tap the save button, save one more cell in the core data 
line 53 is only for a new cell not for editing line 45 is for editing if I can put a photo here then I can explain well 

Could you first show exactly all the print messages you get, in their exact order.
If you can post a photo, that may help.
You are probably new to the forum, so welcome and some advices.

When you copy code, use the Paste and Match Style to avoid extra blank lines that make code hard to read.
And use the code formatter tool (<>).

Code Block
import UIKit
import CoreData
class ComposeViewController: UIViewController {
let formatter:DateFormatter = {
let formatter = DateFormatter()
formatter.dateFormat = "YYYY년 MM월 dd일 E요일"
formatter.locale = Locale.init(identifier: "koKR")
return formatter
}()
// var segueData: TodoList?
var editSegueData: TodoList?
var itemContent:[NSManagedObject] = []
@IBOutlet weak var dateLabel: UILabel!
@IBOutlet weak var textView: UITextView!
@IBAction func save(sender: AnyObject) {
guard let memo = textView.text , memo.count > 0 else {
alert(message: "텍스트를 입력하세요")
return
}
let date = Date()
let appDelegate = UIApplication.shared.delegate as! AppDelegate
let context = appDelegate.persistentContainer.viewContext
let entity = NSEntityDescription.entity(forEntityName: "TodoList", in: context)!
let theTitle = NSManagedObject(entity: entity, insertInto: context)
let checkBoxBool = false
if let target = editSegueData {
print("target.content = \(target.content) \(target.insertDate)")
target.content = memo
do {
try context.save()
// itemContent.append(theTitle)
}
catch {
print("There was a error in saving")
}
NotificationCenter.default.post(name:ComposeViewController.memoDidChange, object: nil)
} else {
theTitle.setValue(memo, forKey: "content")
theTitle.setValue(date, forKey: "insertDate")
theTitle.setValue(checkBoxBool, forKey: "checkBox")
do {
try context.save()
// itemContent.append(theTitle)
}
catch {
print("There was a error in saving")
}
}
NotificationCenter.default.post(name: ComposeViewController.newMemoDidInsert, object: nil)
dismiss(animated: true, completion: nil)
}
@IBAction func cancelButton(_ sender: Any) {
dismiss(animated: true, completion: nil)
}
override func viewDidLoad() {
super.viewDidLoad()
dateLabel.text = formatter.string(from: Date())
if let memo = editSegueData {
navigationItem.title = "EDIT"
textView.text = memo.content
} else {
navigationItem.title = "NEW"
textView.text = ""
}
// Do any additional setup after loading the view.
}
/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// Get the new view controller using segue.destination.
// Pass the selected object to the new view controller.
}
*/
}
extension ComposeViewController {
static let newMemoDidInsert = Notification.Name("newMemoDidInsert")
static let memoDidChange = Notification.Name(rawValue: "memoDidChange")
}


Now to your questions:

when I edit my text and save it it works well

Do you mean line 45 is executed correctly ?

but the nil cell is added on the table at the same time 

Which table ? There is no tableView in your code.


when I add a new text

Where in code do you add new text ?

then it never make a nil cell but only edit works wrong

please explain what you expect and what you get, precisely.
line 45 works wrong
editing is working well but when I tap the save button, save one more cell in the core data
line 53 is only for a new cell not for editing line 45 is for editing if I can put a photo here then I can explain well
Accepted Answer

line 45 works wrong 
editing is working well but when I tap the save button, save one more cell in the core data 
line 53 is only for a new cell not for editing line 45 is for editing if I can put a photo here then I can explain well 

Could you first show exactly all the print messages you get, in their exact order.
If you can post a photo, that may help.
sorry I could not find that how to add a picture on here so I post it on the StackOverFlow so if you are okay check it on there
here is the link

https://stackoverflow.com/questions/63487400/swift-core-data-value-updating-problem-when-did-i-update-a-value-then-an-empty
when I add an edit data in the Core data then add another nil cell on the Table
 
 
Q