I am creating singleton like shown below:
static MyType* shared = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
shared = [self new];
});
return shared;
I know that the code in the block will be executed once and self
will be nil
at that point, so [self new]
will be equal to [MyType new]
. But then I was thinking about situation, when I call [self new]
in a block that is not for singleton purposes and can be called more than once.
Will [self new]
act like [MyType new]
or a block will capture self
? Is it a right way to create new instance of MyType
using [self new]
? What are benefits of using [self new]
instead of [MyType new]
?