我正在尝试根据他们想要保存的列表保存到不同的核心数据对象。
是否可以创建一个传入类型的函数,并根据它保存到正确的列表中?
let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
if index == 0 {
let task = TodayTask(context: context)
task.name = taskText.text! // Do this in function insead
task.adress = addressField.text // Do this in function insead
task.date = userDate // Do this in function insead
}else if listIndex.selectedSegmentIndex == 1 {
let task = WeekTask(context: context)
task.name = taskText.text!
}else {
let task = Task(context: context)
task.name = taskText.text!
func saveToCoreData(task: SomeList){
let task = SomeList(context: context) // The correct list based on what the user chooses
task.name = taskText.text!
task.adress = addressField.text
task.date = userDate
}
答案 0 :(得分:1)
您应该使用一种称为动态调度的技术。例如:
func accept(value: String) {
//This to process a string
}
func accept(value: Int){
//This called to process an Int
}
然后你只需调用accept(value:myValue),该函数将动态调度到正确的函数。