尝试使用元组保存时核心数据错误

时间:2018-05-03 17:29:25

标签: ios swift xcode core-data

尝试将字符串保存到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;给定type = JobNote; value =(entity:JobNote; id:0x1c002f080; data:{       job =“0xd000000000580000”;       note = nil;   })'

我有一个名为 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)
        }
    }

然后我有一个注释页面,其中包含一个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
    }
}

这是我的新笔记VC:

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

1 个答案:

答案 0 :(得分:2)

问题出在这里

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

 }