Skip to content

Commit

Permalink
1、修复单例模式可能产生多个对象的问题;
Browse files Browse the repository at this point in the history
  • Loading branch information
lzj committed Apr 14, 2020
1 parent c7f27c5 commit 16f9d11
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 12 deletions.
2 changes: 1 addition & 1 deletion ZJLog.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = "ZJLog"
s.version = "1.0.8"
s.version = "1.0.9"
s.summary = "Log redirection output tool for iOS, support for c、c++、m、mm code files."
s.description = <<-DESC
Log redirection output tool for iOS, you can set the Log level, redirect output to the proxy interface, save logs to the sandbox, support for c、c++、m、mm code files, and more.
Expand Down
4 changes: 2 additions & 2 deletions ZJLog.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,7 @@
"@loader_path/Frameworks",
);
MACH_O_TYPE = mh_dylib;
MARKETING_VERSION = 1.0.9;
MARKETING_VERSION = 1.1.0;
ONLY_ACTIVE_ARCH = YES;
PRODUCT_BUNDLE_IDENTIFIER = com.eafy.ZJLog;
PRODUCT_NAME = "$(TARGET_NAME:c99extidentifier)";
Expand Down Expand Up @@ -433,7 +433,7 @@
"@loader_path/Frameworks",
);
MACH_O_TYPE = mh_dylib;
MARKETING_VERSION = 1.0.9;
MARKETING_VERSION = 1.1.0;
ONLY_ACTIVE_ARCH = NO;
PRODUCT_BUNDLE_IDENTIFIER = com.eafy.ZJLog;
PRODUCT_NAME = "$(TARGET_NAME:c99extidentifier)";
Expand Down
25 changes: 16 additions & 9 deletions ZJLog/Singleton.h
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
//
// Singleton.h
// ZJLog
// JMSingleton.h
// JMSmartUtils
//
// Created by lzj<lizhijian_21@163.com> on 2019/1/8.
// Copyright © 2019 ZJ. All rights reserved.
// Created by lzj<lizhijian_21@163.com> on 2018/5/11.
// Copyright © 2018年 concox. All rights reserved.
//
//单例模式宏,头文件宏:singleton_h(name),实现文件宏:singleton_m(name)。
//比如单列类名为:shareAudioTool,头文件加入singleton_h(AudioTool),实现文件:singleton_m(AudioTool);
Expand All @@ -14,8 +14,9 @@
}
*/

#ifndef Singleton_h
#define Singleton_h
#ifndef JMSingleton_h
#define JMSingleton_h
#import <objc/message.h>

// ## : 连接字符串和参数
#define singleton_h(name) + (instancetype _Nullable)shared##name;
Expand All @@ -26,6 +27,7 @@
static id _instance; \
+ (instancetype)allocWithZone:(struct _NSZone *)zone \
{ \
if (_instance) return _instance; \
static dispatch_once_t onceToken; \
dispatch_once(&onceToken, ^{ \
_instance = [super allocWithZone:zone]; \
Expand All @@ -34,6 +36,7 @@ return _instance; \
} \
+ (instancetype)shared##name \
{ \
if (_instance) return _instance; \
static dispatch_once_t onceToken; \
dispatch_once(&onceToken, ^{ \
_instance = [[self alloc] init]; \
Expand All @@ -50,11 +53,13 @@ return _instance; \
} \
- (instancetype)init \
{ \
if (_instance) return _instance; \
static dispatch_once_t onceToken; \
dispatch_once(&onceToken, ^{ \
_instance = [super init]; \
if ([self respondsToSelector:@selector(initData)]) { \
[self initData]; \
SEL initDataSel = NSSelectorFromString(@"initData"); \
if ([self respondsToSelector:initDataSel]) { \
((void (*) (id, SEL)) objc_msgSend)(self, initDataSel); \
} \
}); \
return _instance; \
Expand All @@ -66,6 +71,7 @@ return _instance; \
static id _instance; \
+ (id)allocWithZone:(struct _NSZone *)zone \
{ \
if (_instance) return _instance; \
static dispatch_once_t onceToken; \
dispatch_once(&onceToken, ^{ \
_instance = [super allocWithZone:zone]; \
Expand All @@ -74,6 +80,7 @@ return _instance; \
} \
+ (instancetype)shared##name \
{ \
if (_instance) return _instance; \
static dispatch_once_t onceToken; \
dispatch_once(&onceToken, ^{ \
_instance = [[self alloc] init]; \
Expand Down Expand Up @@ -103,4 +110,4 @@ return _instance; \

#endif

#endif /* Singleton_h */
#endif /* JMSingleton_h */

0 comments on commit 16f9d11

Please sign in to comment.