Reputation: 11361
__weak NSString *strin = [[NSString alloc] initWithFormat:@"hey"] ;
NSLog(@"weak %@",strin); //returns weak (null)
__weak NSString *strin =@"hey";
NSLog(@"weak %@",strin); //returns weak hey
What is the difference between both the snippets. Why the second is returning the value when weak is assigned ?
Upvotes: 4
Views: 88
Reputation: 122381
It's because @"hey"
is a constant string literal which will be around forever, whereas the weak reference to the first (allocated) string is not enough to keep it alive.
Upvotes: 4