forum

Home / DeveloperSection / Forums / ObjectiveC: Strange behavior when using NSString and __weak with ARC

ObjectiveC: Strange behavior when using NSString and __weak with ARC

Tarun Kumar 1744 21-Jul-2015

First code and output: 

NSString *text = @"Sunny";

__weak NSString *string0 = text.lowercaseString;

__weak NSString *string1;

string1 = text.lowercaseString;

 

NSLog(@"%@, %@", string0, string1);

 Output: 

(null), sunny

 

But after I move the declaration of string1 above the text, output is different. Here is the code: 

__weak NSString *string1;

NSString *text = @"Sunny";

__weak NSString *string0 = text.lowercaseString;

string1 = text.lowercaseString;

 

NSLog(@"%@, %@", string0, string1);

 Output: 

sunny, sunny

 

I am very confused with the different output:

·         Why string0 and string1 is different in the first case?

·         Why the output of second case is different with the first?

 


Updated on 21-Jul-2015

Can you answer this question?


Answer

1 Answers

Liked By