通过引用访问实例变量是否安全? [英] Is accessing instance variables by reference safe?
本文介绍了通过引用访问实例变量是否安全?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
下面的两个代码段有什么区别?
What is the difference between the two code snippets below:
1.
__block __weak NSMutableArray *arrBlock = self.arr ;
[[AsyncRequest initRequest:url onCompletedBlock:^(NSMutableArray *arr) {
arrBlock = arr;
}]ExecuteRequest];
2.
id __weak weakself = self;
[[AsyncRequest initRequest:url onCompletedBlock:^(NSMutableArray *arr) {
weakself.arr = arr;
}]ExecuteRequest];
Neither of them cause retain cycles, but Apple suggests using the first one. Is there a problem with the second one?
推荐答案
它们有不同的效果.第一个更新本地变量,第二个更新实例变量(属性).
They have different effects. The first updates the local variable and the second updates the instance variable (property).
您要使用哪一个取决于您要发生的事情,但是我怀疑您想要第二个,因为如果该块是异步执行的,则第一个看起来像是无操作.
Which one you want to use depends on what you want to happen, however I suspect you want the second as the first looks like a no-op if that block is executed asynchronously.
这篇关于通过引用访问实例变量是否安全?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文