【发布时间】:2016-06-29 03:40:24
【问题描述】:
我有两个实体“Office”和“Employee”
Office Entity 属性是
officeId - string
officename - string
Employee Entity 属性是
employeename - string
emailid - string
并拥有多对多的关系——办公室与员工的关系是“员工”
func SaveData(){
let entityOffice = NSEntityDescription.entityForName("Office", inManagedObjectContext: context)
let newoffice = NSManagedObject(entity: entityOffice!, insertIntoManagedObjectContext: context)
newoffice.setValue("BUI001", forKey: "officeId")
newoffice.setValue("Home builders", forKey: "officename")
let entityEmployees = NSEntityDescription.entityForName("Employee", inManagedObjectContext:context)
let newemploy = NSManagedObject(entity: entityEmployees!, insertIntoManagedObjectContext: context)
// Populate Address
newemploy.setValue("Tom william", forKey: "employeename")
newemploy.setValue("tomus@yahoo.com", forKey: "emailid")
// Add Address to Person
newoffice.setValue(NSSet(object: newemploy), forKey: "staff")
do {
try newoffice.managedObjectContext?.save()
} catch {
let saveError = error as NSError
print(saveError)
}
}
保存数据工作正常。我不知道如何在有关系的实体之间获取。
如何使用“Office”实体的“officeid”获取“Employee”实体中的所有员工数据。 提前感谢
【问题讨论】:
标签: ios xcode swift fetch entity-relationship