It is simply a matter of readability, consensus and practice. This is really not necessary because:
One. Its value will never be tested. Older singleton implementations used to be famous
+ (id)sharedInstance { static SomeClass *shared = nil; if (shared == nil) shared = [[SomeClass alloc] init]; return shared; }
code - for this method to work, the support variable must be initialized to nil, since if it werenβt the first time, it would falsely omit alloc-init in the if part and return a junk mail pointer, However, with the GCD solution, nil-check is no longer required - GCD processes the right button "execute this code only once".
Two. Nevertheless: static variables are implicitly initialized to zero. Therefore, even if you simply write static id shared; , will initially be nil .
Three. Why can this be good practice? Because, despite the first two reasons that I mentioned, it is even more readable to let the source code reader know that something is explicitly initialized to zero. Or even there may be some inappropriate implementations when the static variables are not automatically auto-initialized, and then this action must be taken.
user529758
source share