我有一個tableview中的事件的名稱,我想救反應(是= 1,無= 2,從動作控制器)與事件名一起的用戶。因此,我創建2個實體建立兩個實體之間的關係?
EventSchedule (eventName: String, eventTime: Transformable)
與one to one
關係eventSchedule
到
EventDec (eventStatus: Integer16)
和one to one
反比關係eventDec
我做了以下
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath){
let alert = UIAlertController(title: "title", message: "msg", preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "yes", style: .default) { _ in
let eventDesc = NSEntityDescription.insertNewObject(forEntityName: "EventDec", into: context)
let eventSch = NSEntityDescription.insertNewObject(forEntityName: "EventSchedule", into: context)
//How do I add relationship property to the above objects so that I can store event name along with response in database?
eventDesc.setValue(1, forKey: "eventStatus")
do
{
try context.save()
print("Record Saved Successfully")
print(eventDesc)
}
catch{}
})
alert.addAction(UIAlertAction(title: "no", style: .default) { _ in
let eventDesc = NSEntityDescription.insertNewObject(forEntityName: "EventDec", into: context)
let eventSch = NSEntityDescription.insertNewObject(forEntityName: "EventSchedule", into: context)
//How do I add relationship property to the above objects so that I can store event name along with response in database?
eventDesc.setValue(2, forKey: "eventStatus")
do
{
try context.save()
print("Record Saved Successfully")
print(eventDesc)
}
catch{}
})
present(alert, animated: true, completion: nil)
}
您可以設置與KVC的關係相同因爲您正在插入數據'eventDesc.setValue(2,forKey:「eventStatus」)'。你必須在那裏使用關係名稱。 –