Ios CoreStore transaction.edit建议使用相同变量名的注释可防止我们误用非事务实例
从文档中,我找到了以下代码:Ios CoreStore transaction.edit建议使用相同变量名的注释可防止我们误用非事务实例,ios,swift,core-data,corestore,Ios,Swift,Core Data,Corestore,从文档中,我找到了以下代码: let jane: MyPersonEntity = // ... CoreStore.perform( asynchronous: { (transaction) -> Void in // WRONG: jane.age = jane.age + 1 // RIGHT: let jane = transaction.edit(jane)! // using the same variable nam
let jane: MyPersonEntity = // ...
CoreStore.perform(
asynchronous: { (transaction) -> Void in
// WRONG: jane.age = jane.age + 1
// RIGHT:
let jane = transaction.edit(jane)! // using the same variable name protects us from misusing the non-transaction instance
jane.age = jane.age + 1
},
completion: { _ in }
)
不确定我们为什么需要这样做//使用相同的变量名可以防止误用非事务实例
斯威夫特建议我使用其中两种:
该建议利用了swift的变量名隐藏功能 Xcode autocomplete仍然会显示两个“jane”,因为另一个同名的也在同一个范围内,尽管永远不能使用,因为它是隐藏的。你在那里选择什么并不重要。由于这个原因,它是处理事务对象最安全的方法,因为它可以防止您意外地使用错误的对象