-1

I had used the following code

- (void)viewDidLoad {
    [super viewDidLoad];
    NSString *string=@"String";
    self.strongStr=string;
    self.weakStr=string;
    self.strongStr=nil;
    NSLog(@"%@",self.weakStr);}

On running the value for weak string should be nil but its printing "String". I Please need a working example with Prniting values so that I can know what is happening with the strong and weak refrences

Anmol Suneja
  • 97
  • 11
  • Don't use string constants to test this stuff. Try `NSMutableString` and build the string. – trojanfoe Sep 18 '15 at 09:59
  • A couple of issues here. First, the local variable `string` is still holding a strong reference so the string won't be released. Also the object probably won't be released straight away - it would be some time after the method exits – Paulw11 Sep 18 '15 at 10:02
  • @Paulw11 Slight Update In Code - (void)viewDidLoad { [super viewDidLoad]; NSString *string=@"String"; self.strongStr=string; self.weakStr=string; string=nil; self.strongStr=nil; NSLog(@"%@",self.weakStr); // Do any additional setup after loading the view, typically from a nib. } -(void)viewDidAppear:(BOOL)animated{ [super viewDidAppear:YES]; NSLog(@"%@",self.weakStr); } Still the same – Anmol Suneja Sep 18 '15 at 10:08
  • possible duplicate of [Differences between strong and weak in Objective-C](http://stackoverflow.com/questions/11013587/differences-between-strong-and-weak-in-objective-c) – Idrees Ashraf Sep 18 '15 at 10:09
  • @IdreesAshraf I need a working example code . I am just a begginer – Anmol Suneja Sep 18 '15 at 10:10
  • As @trojanfoe said, string constants are a bad test since they are compiled in and can't be released as they aren't on the heap. – Paulw11 Sep 18 '15 at 10:14

2 Answers2

0

A weak reference signifies that you don't want to have control over the object's lifetime. The object you are referencing weakly only lives on because at least one other object holds a strong reference to it. Once that is no longer the case, the object gets destroyed and your weak property will automatically get set to nil.

Please go through this thread to understand it more.

Community
  • 1
  • 1
Abhinav
  • 37,684
  • 43
  • 191
  • 309
0

Weak means you don't own that object. Strong means you own that object.

Strong reference means you are referencing with this property/variable. The compiler will take care that any object that you assign to this property will not be destroyed as long as you (or any other object) points to it with a strong reference. Only once you set the property to nil will the object get destroyed (unless one or more other objects also hold a strong reference to it).

Weak reference means you signify that you don't want to have control over the object's lifetime. The object you are referencing weakly only lives on because at least one other object holds a strong reference to it. Once that is no longer the case, the object gets destroyed and your weak property will automatically get set to nil.