Swift核心数据保存到多个实体

时间:2015-01-17 02:04:50

标签: swift core-data xcode6

在我看来,我有除了注释之外的所有UITextfields,这是一个UITextView。当我点击保存时,它会保存所有数据,但不会将注释链接到Teas实体。

 @IBAction func addTea(sender: AnyObject) {
    let appDel: AppDelegate = UIApplication.sharedApplication().delegate as AppDelegate
    let context: NSManagedObjectContext = appDel.managedObjectContext!
    let en = NSEntityDescription.entityForName("Teas", inManagedObjectContext: context)
    var newItem = dataModel(entity: en!, insertIntoManagedObjectContext: context)
    newItem.teaname = teaName.text
    newItem.teatype = teaType.text
    newItem.amount = qty.text
    newItem.temp = temp.text
    newItem.time = time.text
    let en1 = NSEntityDescription.entityForName("Notes", inManagedObjectContext: context)
    let newNote = dataNote(entity: en1!, insertIntoManagedObjectContext: context)
    newNote.note = note.text


    println(note.text)
    context.save(nil)

    self.navigationController?.popViewControllerAnimated(true)

}

dataModel.swift

@objc(dataModel)
class dataModel: NSManagedObject {

@NSManaged var amount: String
@NSManaged var teaname: String
@NSManaged var teatype: String
@NSManaged var temp: String
@NSManaged var time: String
@NSManaged var notes: dataNote

}

notes.swift

@objc(dataNote)
class dataNote: NSManagedObject {

@NSManaged var note: String
@NSManaged var teas: dataModel

}

我认为在看了很多其他例子后我需要这样的事情。

 @IBAction func addTea(sender: AnyObject) {
    let appDel: AppDelegate = UIApplication.sharedApplication().delegate as AppDelegate
    let context: NSManagedObjectContext = appDel.managedObjectContext!
    let en = NSEntityDescription.entityForName("Teas", inManagedObjectContext: context)
    var newItem = dataModel(entity: en!, insertIntoManagedObjectContext: context)
    newItem.teaname = teaName.text
    newItem.teatype = teaType.text
    newItem.amount = qty.text
    newItem.temp = temp.text
    newItem.time = time.text
    newItem.notesRel = noteAdd.text

但我得到'NSString'不是'dataNote'的子类型

0 个答案:

没有答案