I just can not fully understand one moment. Here is the usual implementation of a singleton:

+ (instancetype)sharedInstance { static MyClass *sharedInstance = nil; static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ sharedInstance = [[MyClass alloc] init]; }); return sharedInstance; } 

Then use Singleton 2 times:

 Singleton *inst1 = [Singleton sharedInstance]; Singleton *inst2 = [Singleton sharedInstance]; 

I correctly understand that when you re-execute the line (during the second call):

 static MyClass *sharedInstance = nil; 

Assignment to nil will not occur, because the variable is declared inside a static function? Those. when the static function is called again, will the string be simply ignored?

Where can I read more about this situation? (declaration and initialization of variables inside a static method)

    1 answer 1

    You understand correctly that no assignment will occur and the string will be ignored. However, this is not because the variable is declared inside a static method (strictly speaking, in Objective-C there are no static methods at all), but because this variable has a static modifier. Static has nothing to do with Objective-C, so you can additionally read about it in the materials on the C language, for example, in the standard.