简体   繁体   English

局部变量默认为强还是弱?

[英]Local variables default to strong or weak?

Lets say I have local variable(not property) Obj *x = d, is d's reference count incremented? 假设我有局部变量(不是属性)Obj * x = d,是否增加了引用计数? Or is it default a weak reference? 或者它是默认的弱参考?

Apple's documentation ( Variable Qualifiers section) said: Apple的文档( Variable Qualifiers部分)说:

https://developer.apple.com/library/ios/releasenotes/ObjectiveC/RN-TransitioningToARC/Introduction/Introduction.html https://developer.apple.com/library/ios/releasenotes/ObjectiveC/RN-TransitioningToARC/Introduction/Introduction.html

__strong is the default. __strong是默认值。 An object remains “alive” as long as there is a strong pointer to it. 只要存在指向它的强指针,对象就会保持“活着”。

Say you write 说你写

__weak NSMutableArray* myArray = [[NSMutableArray alloc] init];

What happens? 怎么了? What happens is that the only reference to that array is in a weak variable, which means it gets deallocated immediately and myArray is set to nil. 会发生的是,对该数组的唯一引用是在弱变量中,这意味着它立即被释放并且myArray被设置为nil。 Now say you write 现在说你写

NSMutableArray* myArray = [[NSMutableArray alloc] init];

What would happen if the default is "weak"? 如果默认值为“弱”会发生什么? Does that answer your question? 这是否回答你的问题?

They are strong by default. 它们在默认情况下很强大。

docs 文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM