EasyMapping不会将CoreData映射到一个到多个关系对象

时间:2016-03-09 17:13:41

标签: ios swift core-data

我使用EasyMapping库从JSON到我的对象解组 CoreData对象。对象平衡与Transaction有很多关系,这是我的nsmanaggedobject类:

class Balance: NSManagedObject, EKManagedMappingProtocol {

static func objectMapping() -> EKManagedObjectMapping! {
    return EKManagedObjectMapping.init(forEntityName: String(self), withBlock: { (mapping) -> Void in

        mapping.mapKeyPath("balance.amount", toProperty: "amount")

        mapping.hasMany(Transaction.self, forKeyPath: "transactions")
    })
}

}

这是我的翻译位:

        let b = Balance.MR_createEntityInContext(localContext)

        EKMapper.fillObject(b, fromExternalRepresentation: parsedInfo, withMapping: Balance.objectMapping())

我的实体创建得很好,但没有关系。 不胜感激任何关于为什么不起作用的想法

1 个答案:

答案 0 :(得分:2)

找到解决方案,您需要从EKManagedObjectModel继承实体类,如下所示

class Balance: EKManagedObjectModel {

static func objectMapping() -> EKManagedObjectMapping! {
return EKManagedObjectMapping.init(forEntityName: String(self), withBlock: { (mapping) -> Void in

    mapping.mapKeyPath("balance.amount", toProperty: "amount")

    mapping.hasMany(Transaction.self, forKeyPath: "transactions")
    })
  }
}

和翻译将是

        MagicalRecord.saveWithBlock({ (localContext) -> Void in

        let b = Balance.objectWithProperties(parsedPersonInfo, inContext: localContext)
 ...
}