Ankish Jain
Ankish Jain

Reputation: 11361

iOS/iPhone ARC memory management

 __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

Answers (2)

Mansi Panchal
Mansi Panchal

Reputation: 2357

Check below snap and you will get your answer your self.

enter image description here

Upvotes: 1

trojanfoe
trojanfoe

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

Related Questions