9
9
#import " LKDBUtils.h"
10
10
11
11
@interface LKDateFormatter : NSDateFormatter
12
- @property (nonatomic , strong ) NSRecursiveLock * lock;
12
+ @property (nonatomic , assign ) dispatch_semaphore_t lock;
13
13
@end
14
14
15
15
@implementation LKDateFormatter
16
- - (id )init {
16
+
17
+ - (instancetype )init {
17
18
self = [super init ];
18
19
if (self) {
19
- self.lock = [[ NSRecursiveLock alloc ] init ] ;
20
+ self.lock = dispatch_semaphore_create ( 1 ) ;
20
21
self.generatesCalendarDates = YES ;
21
22
self.dateStyle = NSDateFormatterNoStyle;
22
23
self.timeStyle = NSDateFormatterNoStyle;
@@ -26,43 +27,62 @@ - (id)init {
26
27
if (locale) {
27
28
[self setLocale: locale];
28
29
}
30
+ if ([LKDBUtils respondsToSelector: @selector (onCreateWithDateFormatter: )]) {
31
+ [LKDBUtils onCreateWithDateFormatter: self ];
32
+ }
29
33
}
30
34
return self;
31
35
}
36
+
32
37
// 防止 iOS5 多线程 格式化时间时 崩溃
33
38
- (NSDate *)dateFromString : (NSString *)string {
34
- [ _lock lock ] ;
39
+ dispatch_semaphore_wait ( _lock, DISPATCH_TIME_FOREVER) ;
35
40
NSDate *date = [super dateFromString: string];
36
- [ _lock unlock ] ;
41
+ dispatch_semaphore_signal ( _lock) ;
37
42
return date;
38
43
}
44
+
39
45
- (NSString *)stringFromDate : (NSDate *)date {
40
- [ _lock lock ] ;
46
+ dispatch_semaphore_wait ( _lock, DISPATCH_TIME_FOREVER) ;
41
47
NSString *string = [super stringFromDate: date];
42
- [ _lock unlock ] ;
48
+ dispatch_semaphore_signal ( _lock) ;
43
49
return string;
44
50
}
51
+
45
52
@end
46
53
47
54
@interface LKNumberFormatter : NSNumberFormatter
48
55
49
56
@end
50
57
51
58
@implementation LKNumberFormatter
59
+
60
+ - (instancetype )init {
61
+ self = [super init ];
62
+ if (self) {
63
+ if ([LKDBUtils respondsToSelector: @selector (onCreateWithNumberFormatter: )]) {
64
+ [LKDBUtils onCreateWithNumberFormatter: self ];
65
+ }
66
+ }
67
+ return self;
68
+ }
69
+
52
70
- (NSString *)stringFromNumber : (NSNumber *)number {
53
71
NSString *string = [number stringValue ];
54
72
if (!string) {
55
73
string = [NSString stringWithFormat: @" %lf " , [number doubleValue ]];
56
74
}
57
75
return string;
58
76
}
77
+
59
78
- (NSNumber *)numberFromString : (NSString *)string {
60
79
NSNumber *number = [super numberFromString: string];
61
80
if (!number) {
62
81
number = @(string.doubleValue );
63
82
}
64
83
return number;
65
84
}
85
+
66
86
@end
67
87
68
88
@implementation LKDBUtils
@@ -125,6 +145,7 @@ + (NSString *)getDocumentPath {
125
145
return homePath;
126
146
#endif
127
147
}
148
+
128
149
+ (NSString *)getDirectoryForDocuments : (NSString *)dir {
129
150
NSString *dirPath = [[self getDocumentPath ] stringByAppendingPathComponent: dir];
130
151
BOOL isDir = NO ;
@@ -137,23 +158,29 @@ + (NSString *)getDirectoryForDocuments:(NSString *)dir {
137
158
}
138
159
return dirPath;
139
160
}
161
+
140
162
+ (NSString *)getPathForDocuments : (NSString *)filename {
141
163
return [[self getDocumentPath ] stringByAppendingPathComponent: filename];
142
164
}
165
+
143
166
+ (NSString *)getPathForDocuments : (NSString *)filename inDir : (NSString *)dir {
144
167
return [[self getDirectoryForDocuments: dir] stringByAppendingPathComponent: filename];
145
168
}
169
+
146
170
+ (BOOL )isFileExists : (NSString *)filepath {
147
171
return [[NSFileManager defaultManager ] fileExistsAtPath: filepath];
148
172
}
173
+
149
174
+ (BOOL )deleteWithFilepath : (NSString *)filepath {
150
175
return [[NSFileManager defaultManager ] removeItemAtPath: filepath error: nil ];
151
176
}
177
+
152
178
+ (NSArray *)getFilenamesWithDir : (NSString *)dir {
153
179
NSFileManager *fileManager = [NSFileManager defaultManager ];
154
180
NSArray *fileList = [fileManager contentsOfDirectoryAtPath: dir error: nil ];
155
181
return fileList;
156
182
}
183
+
157
184
+ (BOOL )checkStringIsEmpty : (NSString *)string {
158
185
if (string == nil ) {
159
186
return YES ;
@@ -179,6 +206,7 @@ + (NSDateFormatter *)getDBDateFormat {
179
206
});
180
207
return format;
181
208
}
209
+
182
210
+ (NSString *)stringWithDate : (NSDate *)date {
183
211
NSDateFormatter *formatter = [self getDBDateFormat ];
184
212
NSString *datestr = [formatter stringFromDate: date];
@@ -187,11 +215,13 @@ + (NSString *)stringWithDate:(NSDate *)date {
187
215
}
188
216
return datestr;
189
217
}
218
+
190
219
+ (NSDate *)dateWithString : (NSString *)str {
191
220
NSDateFormatter *formatter = [self getDBDateFormat ];
192
221
NSDate *date = [formatter dateFromString: str];
193
222
return date;
194
223
}
224
+
195
225
+ (NSNumberFormatter *)numberFormatter {
196
226
static NSNumberFormatter *numberFormatter = nil ;
197
227
static dispatch_once_t onceToken;
@@ -200,6 +230,7 @@ + (NSNumberFormatter *)numberFormatter {
200
230
});
201
231
return numberFormatter;
202
232
}
233
+
203
234
@end
204
235
205
236
inline NSString *LKSQLTypeFromObjcType (NSString *objcType) {
0 commit comments