1

In viewDidUnload where I do set the IBOutlets to nil, I also set the NSString objects to nil. These which were (nonatomic,strong). Does it make sense at all to set NSString objects to nil?

Monolo
  • 18,205
  • 17
  • 69
  • 103
NCFUSN
  • 1,624
  • 4
  • 28
  • 44

4 Answers4

3

Notice that you don't set any objects to nil. What you do set to nil is the pointer to one of your objects, so that it does not point to anything any more.

This means that if you have other instance variables in other objects pointing to your string, they will not see any difference in the object - they will still point to the same old string, with the same, unaltered content.

However, to answer your question: It does make sense to nil out your variables, but if and when your view controller is deallocated, then the instance variables will also be deallocated. So if your strings are small, there may not be a pressing need to nil them out in viewDidUnload.

Monolo
  • 18,205
  • 17
  • 69
  • 103
1

viewDidUnload, while it is still around, should be used to remove anything that you can re-create in loadView or viewDidLoad.

If the string properties are part of your view controller's model, and can't be recreated, then don't remove them.

The method is not guaranteed to be called - it will only be called when a view controller is offscreen, but the system has come under memory pressure. It is not like viewWillAppear or viewDidDisappear. It should not be confused with your view controller's dealloc method.

jrturton
  • 118,105
  • 32
  • 252
  • 268
0

If you are manually managing memory then you don't have to set the outlet itself to nil, only the object linked to it.

For example, if you have @property (nonatomic,strong)IBOutlet UIImageView *imageView;

you only need to set the image view to nil - imageView = nil;

Mick MacCallum
  • 129,200
  • 40
  • 280
  • 281
0

-viewDidUnload is meant only for releasing objects that are created in -viewDidLoad or -loadView, including IBOutlets.

If you do not allocate these strings in -loadView or -viewDidLoad then it is wrong to release them in -viewDidUnload.

Darren
  • 25,520
  • 5
  • 61
  • 71