Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 16f9d11

Browse files
author
lzj
committedApr 14, 2020
1、修复单例模式可能产生多个对象的问题;
1 parent c7f27c5 commit 16f9d11

File tree

3 files changed

+19
-12
lines changed

3 files changed

+19
-12
lines changed
 

‎ZJLog.podspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Pod::Spec.new do |s|
22
s.name = "ZJLog"
3-
s.version = "1.0.8"
3+
s.version = "1.0.9"
44
s.summary = "Log redirection output tool for iOS, support for c、c++、m、mm code files."
55
s.description = <<-DESC
66
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.

‎ZJLog.xcodeproj/project.pbxproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -402,7 +402,7 @@
402402
"@loader_path/Frameworks",
403403
);
404404
MACH_O_TYPE = mh_dylib;
405-
MARKETING_VERSION = 1.0.9;
405+
MARKETING_VERSION = 1.1.0;
406406
ONLY_ACTIVE_ARCH = YES;
407407
PRODUCT_BUNDLE_IDENTIFIER = com.eafy.ZJLog;
408408
PRODUCT_NAME = "$(TARGET_NAME:c99extidentifier)";
@@ -433,7 +433,7 @@
433433
"@loader_path/Frameworks",
434434
);
435435
MACH_O_TYPE = mh_dylib;
436-
MARKETING_VERSION = 1.0.9;
436+
MARKETING_VERSION = 1.1.0;
437437
ONLY_ACTIVE_ARCH = NO;
438438
PRODUCT_BUNDLE_IDENTIFIER = com.eafy.ZJLog;
439439
PRODUCT_NAME = "$(TARGET_NAME:c99extidentifier)";

‎ZJLog/Singleton.h

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
//
2-
// Singleton.h
3-
// ZJLog
2+
// JMSingleton.h
3+
// JMSmartUtils
44
//
5-
// Created by lzj<lizhijian_21@163.com> on 2019/1/8.
6-
// Copyright © 2019 ZJ. All rights reserved.
5+
// Created by lzj<lizhijian_21@163.com> on 2018/5/11.
6+
// Copyright © 2018年 concox. All rights reserved.
77
//
88
//单例模式宏,头文件宏:singleton_h(name),实现文件宏:singleton_m(name)。
99
//比如单列类名为:shareAudioTool,头文件加入singleton_h(AudioTool),实现文件:singleton_m(AudioTool);
@@ -14,8 +14,9 @@
1414
}
1515
*/
1616

17-
#ifndef Singleton_h
18-
#define Singleton_h
17+
#ifndef JMSingleton_h
18+
#define JMSingleton_h
19+
#import <objc/message.h>
1920

2021
// ## : 连接字符串和参数
2122
#define singleton_h(name) + (instancetype _Nullable)shared##name;
@@ -26,6 +27,7 @@
2627
static id _instance; \
2728
+ (instancetype)allocWithZone:(struct _NSZone *)zone \
2829
{ \
30+
if (_instance) return _instance; \
2931
static dispatch_once_t onceToken; \
3032
dispatch_once(&onceToken, ^{ \
3133
_instance = [super allocWithZone:zone]; \
@@ -34,6 +36,7 @@ return _instance; \
3436
} \
3537
+ (instancetype)shared##name \
3638
{ \
39+
if (_instance) return _instance; \
3740
static dispatch_once_t onceToken; \
3841
dispatch_once(&onceToken, ^{ \
3942
_instance = [[self alloc] init]; \
@@ -50,11 +53,13 @@ return _instance; \
5053
} \
5154
- (instancetype)init \
5255
{ \
56+
if (_instance) return _instance; \
5357
static dispatch_once_t onceToken; \
5458
dispatch_once(&onceToken, ^{ \
5559
_instance = [super init]; \
56-
if ([self respondsToSelector:@selector(initData)]) { \
57-
[self initData]; \
60+
SEL initDataSel = NSSelectorFromString(@"initData"); \
61+
if ([self respondsToSelector:initDataSel]) { \
62+
((void (*) (id, SEL)) objc_msgSend)(self, initDataSel); \
5863
} \
5964
}); \
6065
return _instance; \
@@ -66,6 +71,7 @@ return _instance; \
6671
static id _instance; \
6772
+ (id)allocWithZone:(struct _NSZone *)zone \
6873
{ \
74+
if (_instance) return _instance; \
6975
static dispatch_once_t onceToken; \
7076
dispatch_once(&onceToken, ^{ \
7177
_instance = [super allocWithZone:zone]; \
@@ -74,6 +80,7 @@ return _instance; \
7480
} \
7581
+ (instancetype)shared##name \
7682
{ \
83+
if (_instance) return _instance; \
7784
static dispatch_once_t onceToken; \
7885
dispatch_once(&onceToken, ^{ \
7986
_instance = [[self alloc] init]; \
@@ -103,4 +110,4 @@ return _instance; \
103110

104111
#endif
105112

106-
#endif /* Singleton_h */
113+
#endif /* JMSingleton_h */

0 commit comments

Comments
 (0)
Please sign in to comment.