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?
-
See this question: http://stackoverflow.com/questions/1210776/objective-c-difference-between-setting-nil-and-releasing – buildsucceeded Sep 05 '12 at 20:01
4 Answers
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
.

- 18,205
- 17
- 69
- 103
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.

- 118,105
- 32
- 252
- 268
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;

- 129,200
- 40
- 280
- 281
-
-
@jrturton Thanks for calling me on that one, I don't know what I was thinking. – Mick MacCallum Sep 05 '12 at 20:02
-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
.

- 25,520
- 5
- 61
- 71
-
-
what about the IButlets? are they created in "viewDidUnload" method? – Harshit Gupta Sep 17 '12 at 12:40