gpt4 book ai didi

ios - CoreStore transaction.edit 建议使用相同变量名的注释可以防止我们误用非事务实例

转载 作者:行者123 更新时间:2023-11-28 13:41:50 24 4
gpt4 key购买 nike

我从文档中找到这段代码:

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 建议我使用其中两个:

enter image description here

最佳答案

该建议利用了 swift 具有的变量名阴影功能。

Xcode 自动完成仍会向您显示两个“jane”,因为另一个名为 name 的也在同一范围内,尽管永远无法使用 - 因为它被遮蔽了。你在那里选择什么并不重要。由于这个原因,它是处理事务对象的最安全方法,因为它可以防止您意外使用错误的对象。

关于ios - CoreStore transaction.edit 建议使用相同变量名的注释可以防止我们误用非事务实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55920699/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com