我很努力地将我的逻辑转换为Swift 2,从1.2中的工作正常,并希望有人能够启发我吗?Swift 2尝试抓取,而不是如果让
这是雨燕1.2,让我要么更新记录或插入一个新的...
if let people: [People] = DataManager.getContext().executeFetchRequest(fetchRequest) as? [People] {
// stuff here
DataManager.saveManagedContext()
}
else if let person: People = NSEntityDescription.insertNewObjectForEntityForName(entityNamePeople, inManagedObjectContext: DataManager.getContext()) as? People {
// stuff here
DataManager.saveManagedContext()
}
但现在斯威夫特2我有这个这是我觉得一样的第一个if语句中1.2代码并允许更新记录,但不再更多if/else如何操作插入部分?
do {
let people:[People] = try DataManager.getContext().executeFetchRequest(fetchRequest) as! [People]
// stuff here
DataManager.saveManagedContext()
} catch {
}
感谢advanve
感谢快速回复。这部分解决了这个问题,但是如果产生警告,其他部分在'try'表达式中没有发生调用抛出函数的情况。 – Piginhat
啊,是的,这是我在答案中的注意点:我没有检查'DataManager.getContext()。executeFetchRequest'和'NSEntityDescription.insertNewObjectForEntityForName'实际* throw *,我只是使用你的示例代码作为基础。只要移除不会抛出的方法的“try”即可。 – Moritz
谢谢!这真的让我感到t as不安,因为从应用程序中完全感觉完全失去了这种错误!我现在看到了这个功能,而那个功能没有。我试图接受你的答案,但论坛报告我没有足够的声誉.... – Piginhat