Home » IOS » IOS single singleton macro

IOS single singleton macro

//
/ / Single.h
/ / TestCoreLocation
/ /
By ZongGen on 15/9/27. / / Created
/ / Copyright (c) XuZonggen. All rights reserved. 2015
/ /
#define SINGLEINTERFACE (className) + (instancetype) share##className;
#if __has_feature (objc_arc)
//ARC
#define, SINGLEIMPLEMENTATION (className), static, className, *instance_;

+ (instancetype) share##className
{
if (... Instance_) {

instance_ = [[self, alloc], init];
}

return instance_;
}

+ (instancetype) allocWithZone: (struct _NSZone *) zone
{

static dispatch_once_t onceTocken;

dispatch_once (&onceTocken, ^{, )

instance_ = [super allocWithZone:zone];
});

return instance_;
}
#else
/ / MRC
#define, SINGLEIMPLEMENTATION (className), static, className, *instance_;

+ (instancetype) share##className
{
if (... Instance_) {

instance_ = [[self, alloc], init];
}

return instance_;
}

+ (instancetype) allocWithZone: (struct _NSZone *) zone
{

, static, dispatch_once_t, onceTocken;

dispatch_once (&onceTocken, ^{, )

instance_ = [super allocWithZone:zone];
, });

return instance_;
}
- (oneway, void) release
{}
- (instancetype) retain
{
return self;
}
- (instancetype) autorelease
{
return self;
}
- (NSUInteger) retainCount
{
return MAXFLOAT;
}
#endif

Latest