Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

__weak reference is still a mystery for me

Tags:

objective-c

 NSString *myString = [NSString stringWithFormat:@"string1"];
__weak NSString *myString1 = myString;
myString= nil;
NSLog(@"%@, %@",myString,myString1);

I was expecting null , null. But the output is string1, (null). Why is myString1 still holding the value as myString is set to nil?

like image 902
Atul Chambyal Avatar asked Dec 15 '22 09:12

Atul Chambyal


1 Answers

Weak references only get zeroed when the object is deallocated. That object is not immediately deallocated (it's probably in an autorelease pool here, though there are many other reasons something might be held onto in different situations), so the reference stays alive.

like image 182
Chuck Avatar answered Mar 01 '23 15:03

Chuck