ios - What happens if the init method of a singleton class is called before the +sharedInstance method..? -


what happens if init method of singleton class called before +sharedinstance method..? result in new object , if not how same instance returned ? fact static variable declared inside sharedinstance have effect on overall outcome..?

+ (libraryapi*)sharedinstance {     // 1     static libraryapi *_sharedinstance = nil;      // 2     static dispatch_once_t oncepredicate;      // 3     dispatch_once(&oncepredicate, ^{         _sharedinstance = [[libraryapi alloc] init];     });     return _sharedinstance; } 

in objective-c init method of class method 'init' prefix. it's 1 of biggest differences between objc , swift. if call init method bad you'll no doubt init new instance when init method return successfully.

a static variable inside function keeps value between invocations. every time method sharedinstance called give same instance. more static variable check out here


Comments

Popular posts from this blog

python - No exponential form of the z-axis in matplotlib-3D-plots -

php - Best Light server (Linux + Web server + Database) for Raspberry Pi -

c# - "Newtonsoft.Json.JsonSerializationException unable to find constructor to use for types" error when deserializing class -