I have some experience of programming in Java and Python. I am absolutely confused by code for creation of singleton, which first appears in Camera chapter and then MCVS chapter.
It goes like this.
- (BNRFeedStore *)sharedStore
static BNRFeedStore *feedStore = nil;
feedStore = [[BNRFeedStore alloc]init];
My reading of code is like this,
- Assign feedstore to nil
- Check if feedstore is not assigned to any object
- if not then allocate and initialise new object and point feedstore to that object
- return feedstore
Why is there an assignment to nil at the very beginning of code? Won’t second “if condition” be always true after that? If that is the case then we are returning a pointer to new/different object each time, how is this singleton then?
What am I missing? I feel like I am missing something very basic here.