I have been trying to understand the difference between Strong and Weak references in iOS. What I did to understand is:
//.h File
@property(nonatomic,strong) NSString* myStrongString;
@property(nonatomic,weak) NSString* myWeakString;
//.m File
- (void)viewDidLoad
{
[super viewDidLoad];
[self assignTempString];
// Do any additional setup after loading the view, typically from a nib.
}
-(void)assignTempString{
self.myStrongString = [[NSString alloc] initWithString:@"Varun Mehta"];
}
- (IBAction)printAssignedString:(id)sender {
NSLog(@"Object will have strong reference so it will print my name==%@",self.myStrongString);
}
According to my understanding when I repeat the above step by using myWeakString it should print null. But its still printing my name. Anybody having any idea why its happening.
But when I replace [[NSString alloc] initWithString:@"Varun Mehta"]
with [NSString stringWithFormat:@"Varun Mehta"]
or [[NSString alloc] initWithFormat:@"Varun Mehta"]
result is coming as I have expected.