Local variables default to strong or weak?

时间:2015-07-28 16:39:19

标签: ios objective-c automatic-ref-counting

Lets say I have local variable(not property) Obj *x = d, is d's reference count incremented? Or is it default a weak reference?

3 个答案:

答案 0 :(得分:4)

Apple's documentation (Variable Qualifiers section) said:

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

__strong is the default. An object remains “alive” as long as there is a strong pointer to it.

答案 1 :(得分:2)

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. Now say you write

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

What would happen if the default is "weak"? Does that answer your question?

答案 2 :(得分:1)

They are strong by default.

docs