尝试将字符串保存到newJobNote类中的coreData时出现此错误。似乎在private func createNote()行上的函数let tuple = CoreDataManager.shared.createJobNote(jobNote: jobNote, job: job)中崩溃了
这是错误:
2018-05-03 17:37:53.180012+0100拍摄[7022:3228759]***由于未捕获异常“NSInvalidArgumentException”而终止应用程序,原因:“属性的值类型不可接受:property=”note”;所需类型=NSString;给定类型=JobNote;value=(实体:JobNote;id:0x1c002f080;数据:{
job=“0xd00000000580000”;
注=无;
}).'
我有一个名为Job的实体,它存储了一个作业名-然后我将此传递给我的JobNoteVC。
我还有一个名为:JobNote的实体,其属性名为:note。我还为我的核心数据构建了一个结构:

func createJobNote(jobNote: String, job: Job) -> (JobNote?, Error?) {
        let context = persistentContainer.viewContext
        // create note
        let jobNote = NSEntityDescription.insertNewObject(forEntityName: "JobNote", into: context) as! JobNote

        jobNote.job = job

        jobNote.setValue(jobNote, forKey: "note")

        do {
            try context.save()
            return (jobNote, nil)
        } catch let error {
            print ("Failed to add camera:", error)
            return (nil, error)
        }
    }

然后,我有一个note页面,其中包含一个tableView,它将显示存储在coredata中的所有注释:
import UIKit
import CoreData

class notes : UIViewController, UITableViewDelegate, UITableViewDataSource, NewJobNoteControllerDelegate {

    let cellId = "cellId"
    var tableView = UITableView()
    var jobNotesArray = [JobNote]()

    var job: Job? {
        let tabBarController = self.tabBarController as! jobTabController
        return tabBarController.job
    }

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)

        self.tabBarController?.navigationItem.title = "NOTES"
        let addButton = UIBarButtonItem(barButtonSystemItem: .add, target: self, action: #selector(HandleNewNote))
        self.tabBarController?.navigationItem.rightBarButtonItems = [addButton]

        addTableView()
        fetchJobNotes()
    }

    func addTableView() {
        // TABLE VIEW SETUP
        tableView.frame = self.view.frame
        self.view.addSubview(tableView)
        tableView.delegate = self
        tableView.dataSource = self
    }

    private func fetchJobNotes(){
        guard let jobNotes = job?.jobNotes?.allObjects as? [JobNote] else { return }
        self.jobNotesArray = jobNotes
    }

    func didAddJobNote(jobNote: JobNote) {
        jobNotesArray.append(jobNote)
        tableView.reloadData()
    }

    func didEditJobNote(jobNote: JobNote) {
        let row = jobNotesArray.index(of: jobNote)
        let reloadIndexPath = IndexPath(row: row!, section: 0)
        tableView.reloadRows(at: [reloadIndexPath], with: .middle)
    }

    @IBAction func HandleNewNote(sender : UIButton) {
        let newNote = newJobNote()
        newNote.delegate = self
        newNote.job = job
        let navController = UINavigationController(rootViewController: newNote)
        present(navController, animated: true, completion: nil)
    }

    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
         return jobNotesArray.count
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = UITableViewCell(style: .default, reuseIdentifier: nil)
        return cell
    }
}

这是我的新留言:
import CoreData

protocol NewJobNoteControllerDelegate {
    func didAddJobNote(jobNote : JobNote)
    func didEditJobNote(jobNote: JobNote)
}

class newJobNote: UIViewController {

    var delegate: NewJobNoteControllerDelegate?
    var job: Job?

    let noteLabel: UILabel = {
        let label = UILabel()
        label.text = "NOTE"
        label.translatesAutoresizingMaskIntoConstraints = false
        label.textColor = UIColor(red:0.63, green:0.63, blue:0.63, alpha:1.0)
        return label
    }()

    var noteTextField: UITextField = {
        let textField = UITextField()
        textField.placeholder = "Enter Note..."
        textField.tintColor = UIColor(red:0.87, green:0.30, blue:0.32, alpha:1.0)
        textField.translatesAutoresizingMaskIntoConstraints = false
        return textField
    }()


    var jobNote : JobNote? {
        didSet {
            noteTextField.text = jobNote?.note
        }
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = UIColor.white
        let backButton = UIBarButtonItem(barButtonSystemItem: .save, target: self, action: #selector(HandleBack))
        navigationItem.leftBarButtonItem = backButton
    }

    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        navigationItem.title = "NEW NOTE"
    }

    private func createNote() {
        guard let jobNote = noteTextField.text else { return }
        guard let job = self.job else { return }

        let tuple = CoreDataManager.shared.createJobNote(jobNote: jobNote, job: job)

        if let error = tuple.1 {
            print(error)
        } else {
            dismiss(animated: true, completion: {
                self.delegate?.didAddJobNote(jobNote: tuple.0!)
            })
        }
    }

    private func saveNoteChanges() {
        let context = CoreDataManager.shared.persistentContainer.viewContext

        jobNote?.note = noteTextField.text

        do {
            try context.save()
            dismiss(animated: true) {
                self.delegate?.didEditJobNote(jobNote: self.jobNote!)
            }
        } catch let error {
            print ("Failed to save camera changes:", error)
        }
    }

    @IBAction private func HandleBack(sender : UIButton) {
        if jobNote == nil {
            createNote()
            print("back to note and saved")
        } else {
            saveNoteChanges()
            print("back to note and saved")
        }
    }
}

最佳答案

问题就在这里

jobNote.setValue(jobNote, forKey: "note")

不能将实体实例设置为实体内部的属性,请更改参数名
func createJobNote(sendedNote: String, job: Job) -> (JobNote?, Error?) {

  let context = persistentContainer.viewContext
    // create note
    let jobNote = NSEntityDescription.insertNewObject(forEntityName: "JobNote", into: context) as! JobNote

    jobNote.job = job

    jobNote.setValue(sendedNote, forKey: "note")

    do {
        try context.save()
        return (jobNote, nil)
    } catch let error {
        print ("Failed to add camera:", error)
        return (nil, error)
    }

 }

关于ios - 尝试使用元组保存时发生核心数据错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/50160813/

10-08 21:06