I'm trying to create int object variable in singleton class. This my ObjC version:
+(MyClass*) single
{
static MyClass *shareObject = nil;
static dispatch_once_t onceToken;
dispatch_once ( &onceToken, ^{
shareObject = [[self alloc] init];
shareObject.myArray = [NSMutableArray new];
});
return shareObject;
}
Swift version:
class MyClass {
static let sharedInstance = MyClass()
var myArray = Array<Any>()
}
In case of the ObjC version I know myArray is init once. But my question to you guys in case of the Swift version. Does the myArray variable would be init once?
static(or global) stored variable actually currently usesdispatch_onceunder the hood on Apple platforms (compare stackoverflow.com/q/43374222/2976878) – making it more or less the direct equivalent of your Obj-C+singlemethod.