无法解决一对多关系

时间:2018-08-31 00:16:28

标签: swift core-data nsmanagedobject

我尝试了解coredata。顺利进行的是添加Hond和gewicht。整齐地把关系夹在中间。但是,当我稍后想要添加额外的三明治时,我遇到了一个我无法做到的问题。

我看到以下消息:

  

NSManagedObject'不能转换为'Hond'

My enitys

这是我尝试添加权重的代码

guard let appDelegate = UIApplication.shared.delegate as? AppDelegate else { return }
        let context = appDelegate.persistentContainer.viewContext

        let hondEntity = NSEntityDescription.entity(forEntityName: "Hond", in: context)!
        let hond = NSManagedObject(entity: hondEntity, insertInto: context)

        let gewichtHondEntity = NSEntityDescription.entity(forEntityName: "GewichtHond", in: context)!
        let gewichtHond = NSManagedObject(entity: gewichtHondEntity, insertInto: context)

        let nieuwGewicht = GewichtHond(context: context)
        nieuwGewicht.datum = Date()
        nieuwGewicht.gewicht = Double("38")!
        //gewichtHond.setValue(Date(), forKey: "datum")
        //gewichtHond.setValue(Double("38"), forKey: "gewicht")
        Hond.addToGewichten(gewichtHond) //NSManagedObject' is not convertible to 'Hond

        do {
            try context.save()
            print("Saved")
        } catch let error as NSError {
            print("Could not save. \(error), \(error.userInfo)")
        }

我的Hond + CoreDataProperties.swift

    extension Hond {

        @nonobjc public class func fetchRequest() -> NSFetchRequest<Hond> {
            return NSFetchRequest<Hond>(entityName: "Hond")
        }

        @NSManaged public var chipnummer: Int64
        @NSManaged public var dierenarts: String?
        @NSManaged public var geboortedatum: Date
        @NSManaged public var geslacht: Int16
        @NSManaged public var hondId: String?
        @NSManaged public var kleur: String?
        @NSManaged public var naam: String?
        @NSManaged public var orderID: Int16
        @NSManaged public var plaatsVanChip: String?
        @NSManaged public var telefoonnummerDierenarts: String?
        @NSManaged public var vachttype: String?
        @NSManaged public var gewichten: Set<GewichtHond>

    }

    // MARK: Generated accessors for gewichten
    extension Hond {

        @objc(addGewichtenObject:)
        @NSManaged public func addToGewichten(_ value: GewichtHond)

        @objc(removeGewichtenObject:)
        @NSManaged public func removeFromGewichten(_ value: GewichtHond)
/*
       @objc(addGewichten:)
       @NSManaged public func addToGewichten(_ values: NSSet)

       @objc(removeGewichten:)
       @NSManaged public func removeFromGewichten(_ values: NSSet)
*/ 
    }

我如何设法添加额外的吉它呢?

1 个答案:

答案 0 :(得分:2)

该错误有点误导。基本上说gewicht关系的类型是集合而不是数字。这也表明对gewicht中的GewichtHond属性的干扰。

为避免命名混乱,强烈建议使用对方的实体名称来命名关系,并以单数形式的一对一关系和以复数形式的一对多关系来命名。一对多关系应命名为gewichten。 Swift中的类型应该是Set<GewichtHond>而不是未指定的NSSet

要将GewichtHond实例添加到集合或从集合中删除,Hond子类中至少需要这两个访问器方法

// MARK: Generated accessors for gewichten
extension Hond {

    @objc(addGewichtenObject:)
    @NSManaged public func addToGewichten(_ value: GewichtHond)

    @objc(removeGewichtenObject:)
    @NSManaged public func removeFromGewichten(_ value: GewichtHond)

}

现在创建一个Hond实例和一个GewichtHond实例,并将后者添加到关系集中

let hond = NSEntityDescription.insertNewObject(forEntityName: "Hond", into: context) as! Hond
let gewichtHond = NSEntityDescription.insertNewObject(forEntityName: "GewichtHond" , into: context) as! GewichtHond

hond.addToGewichten(gewichtHond)

旁注:在NSManagedObject子类中,点表示法hond1.datum = Date()比KVC方法hond1.setValue(Date(), forKey: "datum")更方便(更安全)。