Reputation: 16132
I have a class called ContentViewControllerPartial
extending UIViewController
. I instantiate it several times using
content = [self.storyboard instantiateViewControllerWithIdentifier:@"ContentViewControllerPartial"];
expecting to get a new instance each time.
In my class I have an instance variable:
@implementation ContentViewController
...
AVPlayer* mPlayerAV;
which is instanciated during viewDidLoad:
- (void)viewDidLoad {
[super viewDidLoad];
...
if ( !mPlayerAV )
{
mPlayerAV = [AVPlayer playerWithURL: videoURL];
}
To my surprise, each of my ContentViewControllerPartial classes shared the same instance of mPlayerAV! This was obvious by the fact that it was not nil on the 2nd and 3rd instancations.
Only when I have turned mPlayerAV to a property, I got the expected behaviour - mPlayerAV was no longer shared between instances, and was nil.
What is going on here? How come a private instance variable is shared between instances?
Upvotes: 0
Views: 103
Reputation: 3812
If you don't put your instance variables in {} it creates a global instead of an instance variable.
The post here does a great job of do's and don'ts for iVars.
Judging by the code provided I believe this explains the odd behavior. I hope that helps.
Upvotes: 1