Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios CoreStore transaction.edit建议使用相同变量名的注释可防止我们误用非事务实例_Ios_Swift_Core Data_Corestore - Fatal编程技术网

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”,因为另一个同名的也在同一个范围内,尽管永远不能使用,因为它是隐藏的。你在那里选择什么并不重要。由于这个原因,它是处理事务对象最安全的方法,因为它可以防止您意外地使用错误的对象