//
// Single.h
// TestCoreLocation
//
// Created by ZongGen on 15/9/27.
// Copyright (c) 2015年 XuZonggen. All rights reserved.
//
#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
iOS单例 单例宏
最新推荐文章于 2023-09-15 19:56:24 发布