Skip to content

Commit 92fc691

Browse files
committed
修复NSNumber相关问题、字典报错问题
1 parent 69e11e4 commit 92fc691

File tree

8 files changed

+67
-96
lines changed

8 files changed

+67
-96
lines changed

MJExtension.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 = "MJExtension"
3-
s.version = "3.0.9"
3+
s.version = "3.0.10"
44
s.ios.deployment_target = '6.0'
55
s.osx.deployment_target = '10.8'
66
s.summary = "A fast and convenient conversion between JSON and model"

MJExtension/MJDictionaryCache.h

Lines changed: 0 additions & 34 deletions
This file was deleted.

MJExtension/MJDictionaryCache.m

Lines changed: 0 additions & 37 deletions
This file was deleted.

MJExtension/MJPropertyType.m

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,24 @@
1010
#import "MJExtension.h"
1111
#import "MJFoundation.h"
1212
#import "MJExtensionConst.h"
13-
#import "MJDictionaryCache.h"
1413

1514
@implementation MJPropertyType
1615

16+
static NSMutableDictionary *types_;
17+
+ (void)initialize
18+
{
19+
types_ = [NSMutableDictionary dictionary];
20+
}
21+
1722
+ (instancetype)cachedTypeWithCode:(NSString *)code
1823
{
1924
MJExtensionAssertParamNotNil2(code, nil);
2025

21-
static const char MJCachedTypesKey = '\0';
22-
23-
MJPropertyType *type = [MJDictionaryCache objectForKey:code forDictId:&MJCachedTypesKey];
26+
MJPropertyType *type = types_[code];
2427
if (type == nil) {
2528
type = [[self alloc] init];
2629
type.code = code;
27-
[MJDictionaryCache setObject:type forKey:code forDictId:&MJCachedTypesKey];
30+
types_[code] = type;
2831
}
2932
return type;
3033
}

MJExtension/NSObject+MJClass.m

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,36 @@
1111
#import "NSObject+MJKeyValue.h"
1212
#import "MJFoundation.h"
1313
#import <objc/runtime.h>
14-
#import "MJDictionaryCache.h"
1514

1615
static const char MJAllowedPropertyNamesKey = '\0';
1716
static const char MJIgnoredPropertyNamesKey = '\0';
1817
static const char MJAllowedCodingPropertyNamesKey = '\0';
1918
static const char MJIgnoredCodingPropertyNamesKey = '\0';
2019

20+
static NSMutableDictionary *allowedPropertyNamesDict_;
21+
static NSMutableDictionary *ignoredPropertyNamesDict_;
22+
static NSMutableDictionary *allowedCodingPropertyNamesDict_;
23+
static NSMutableDictionary *ignoredCodingPropertyNamesDict_;
24+
2125
@implementation NSObject (MJClass)
2226

27+
+ (void)load
28+
{
29+
allowedPropertyNamesDict_ = [NSMutableDictionary dictionary];
30+
ignoredPropertyNamesDict_ = [NSMutableDictionary dictionary];
31+
allowedCodingPropertyNamesDict_ = [NSMutableDictionary dictionary];
32+
ignoredCodingPropertyNamesDict_ = [NSMutableDictionary dictionary];
33+
}
34+
35+
+ (NSMutableDictionary *)dictForKey:(const void *)key
36+
{
37+
if (key == &MJAllowedPropertyNamesKey) return allowedPropertyNamesDict_;
38+
if (key == &MJIgnoredPropertyNamesKey) return ignoredPropertyNamesDict_;
39+
if (key == &MJAllowedCodingPropertyNamesKey) return allowedCodingPropertyNamesDict_;
40+
if (key == &MJIgnoredCodingPropertyNamesKey) return ignoredCodingPropertyNamesDict_;
41+
return nil;
42+
}
43+
2344
+ (void)mj_enumerateClasses:(MJClassesEnumeration)enumeration
2445
{
2546
// 1.没有block就直接返回
@@ -117,16 +138,16 @@ + (void)mj_setupBlockReturnValue:(id (^)())block key:(const char *)key
117138
}
118139

119140
// 清空数据
120-
[[MJDictionaryCache dictWithDictId:key] removeAllObjects];
141+
[[self dictForKey:key] removeAllObjects];
121142
}
122143

123144
+ (NSMutableArray *)mj_totalObjectsWithSelector:(SEL)selector key:(const char *)key
124145
{
125-
NSMutableArray *array = [MJDictionaryCache objectForKey:NSStringFromClass(self) forDictId:key];
146+
NSMutableArray *array = [self dictForKey:key][NSStringFromClass(self)];
126147
if (array) return array;
127148

128149
// 创建、存储
129-
[MJDictionaryCache setObject:array = [NSMutableArray array] forKey:NSStringFromClass(self) forDictId:key];
150+
[self dictForKey:key][NSStringFromClass(self)] = array = [NSMutableArray array];
130151

131152
if ([self respondsToSelector:selector]) {
132153
#pragma clang diagnostic push

MJExtension/NSObject+MJKeyValue.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ - (instancetype)mj_setKeyValues:(id)keyValues context:(NSManagedObjectContext *)
159159
NSString *oldValue = value;
160160

161161
// NSString -> NSNumber
162-
if (type.class == [NSDecimalNumber class]) {
162+
if (type.typeClass == [NSDecimalNumber class]) {
163163
value = [NSDecimalNumber decimalNumberWithString:oldValue];
164164
} else {
165165
value = [numberFormatter_ numberFromString:oldValue];

MJExtension/NSObject+MJProperty.m

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,21 +13,45 @@
1313
#import "MJProperty.h"
1414
#import "MJFoundation.h"
1515
#import <objc/runtime.h>
16-
#import "MJDictionaryCache.h"
1716

1817
#pragma clang diagnostic push
1918
#pragma clang diagnostic ignored "-Wundeclared-selector"
2019
#pragma clang diagnostic ignored "-Warc-performSelector-leaks"
2120

22-
@implementation NSObject (Property)
23-
2421
static const char MJReplacedKeyFromPropertyNameKey = '\0';
2522
static const char MJReplacedKeyFromPropertyName121Key = '\0';
2623
static const char MJNewValueFromOldValueKey = '\0';
2724
static const char MJObjectClassInArrayKey = '\0';
2825

2926
static const char MJCachedPropertiesKey = '\0';
3027

28+
@implementation NSObject (Property)
29+
30+
static NSMutableDictionary *replacedKeyFromPropertyNameDict_;
31+
static NSMutableDictionary *replacedKeyFromPropertyName121Dict_;
32+
static NSMutableDictionary *newValueFromOldValueDict_;
33+
static NSMutableDictionary *objectClassInArrayDict_;
34+
static NSMutableDictionary *cachedPropertiesDict_;
35+
36+
+ (void)load
37+
{
38+
replacedKeyFromPropertyNameDict_ = [NSMutableDictionary dictionary];
39+
replacedKeyFromPropertyName121Dict_ = [NSMutableDictionary dictionary];
40+
newValueFromOldValueDict_ = [NSMutableDictionary dictionary];
41+
objectClassInArrayDict_ = [NSMutableDictionary dictionary];
42+
cachedPropertiesDict_ = [NSMutableDictionary dictionary];
43+
}
44+
45+
+ (NSMutableDictionary *)dictForKey:(const void *)key
46+
{
47+
if (key == &MJReplacedKeyFromPropertyNameKey) return replacedKeyFromPropertyNameDict_;
48+
if (key == &MJReplacedKeyFromPropertyName121Key) return replacedKeyFromPropertyName121Dict_;
49+
if (key == &MJNewValueFromOldValueKey) return newValueFromOldValueDict_;
50+
if (key == &MJObjectClassInArrayKey) return objectClassInArrayDict_;
51+
if (key == &MJCachedPropertiesKey) return cachedPropertiesDict_;
52+
return nil;
53+
}
54+
3155
#pragma mark - --私有方法--
3256
+ (NSString *)propertyKey:(NSString *)propertyName
3357
{
@@ -124,7 +148,7 @@ + (void)mj_enumerateProperties:(MJPropertiesEnumeration)enumeration
124148
#pragma mark - 公共方法
125149
+ (NSMutableArray *)properties
126150
{
127-
NSMutableArray *cachedProperties = [MJDictionaryCache objectForKey:NSStringFromClass(self) forDictId:&MJCachedPropertiesKey];
151+
NSMutableArray *cachedProperties = [self dictForKey:&MJCachedPropertiesKey][NSStringFromClass(self)];
128152

129153
if (cachedProperties == nil) {
130154
cachedProperties = [NSMutableArray array];
@@ -154,7 +178,7 @@ + (NSMutableArray *)properties
154178
free(properties);
155179
}];
156180

157-
[MJDictionaryCache setObject:cachedProperties forKey:NSStringFromClass(self) forDictId:&MJCachedPropertiesKey];
181+
[self dictForKey:&MJCachedPropertiesKey][NSStringFromClass(self)] = cachedProperties;
158182
}
159183

160184
return cachedProperties;
@@ -193,22 +217,22 @@ + (void)mj_setupObjectClassInArray:(MJObjectClassInArray)objectClassInArray
193217
{
194218
[self mj_setupBlockReturnValue:objectClassInArray key:&MJObjectClassInArrayKey];
195219

196-
[[MJDictionaryCache dictWithDictId:&MJCachedPropertiesKey] removeAllObjects];
220+
[[self dictForKey:&MJCachedPropertiesKey] removeAllObjects];
197221
}
198222

199223
#pragma mark - key配置
200224
+ (void)mj_setupReplacedKeyFromPropertyName:(MJReplacedKeyFromPropertyName)replacedKeyFromPropertyName
201225
{
202226
[self mj_setupBlockReturnValue:replacedKeyFromPropertyName key:&MJReplacedKeyFromPropertyNameKey];
203227

204-
[[MJDictionaryCache dictWithDictId:&MJCachedPropertiesKey] removeAllObjects];
228+
[[self dictForKey:&MJCachedPropertiesKey] removeAllObjects];
205229
}
206230

207231
+ (void)mj_setupReplacedKeyFromPropertyName121:(MJReplacedKeyFromPropertyName121)replacedKeyFromPropertyName121
208232
{
209233
objc_setAssociatedObject(self, &MJReplacedKeyFromPropertyName121Key, replacedKeyFromPropertyName121, OBJC_ASSOCIATION_COPY_NONATOMIC);
210234

211-
[[MJDictionaryCache dictWithDictId:&MJCachedPropertiesKey] removeAllObjects];
235+
[[self dictForKey:&MJCachedPropertiesKey] removeAllObjects];
212236
}
213237
@end
214238

MJExtensionExample.xcodeproj/project.pbxproj

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
2DE3CDA51BEF7B3800DA0F2E /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 2DE3CDA31BEF7B3800DA0F2E /* LaunchScreen.storyboard */; };
1717
2DE3CDB01BEF7B3800DA0F2E /* MJExtensionExampleTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 2DE3CDAF1BEF7B3800DA0F2E /* MJExtensionExampleTests.m */; };
1818
2DE3CDBB1BEF7B3800DA0F2E /* MJExtensionExampleUITests.m in Sources */ = {isa = PBXBuildFile; fileRef = 2DE3CDBA1BEF7B3800DA0F2E /* MJExtensionExampleUITests.m */; };
19-
2DE3CDE01BEF7B9F00DA0F2E /* MJDictionaryCache.m in Sources */ = {isa = PBXBuildFile; fileRef = 2DE3CDCA1BEF7B9F00DA0F2E /* MJDictionaryCache.m */; settings = {ASSET_TAGS = (); }; };
2019
2DE3CDE11BEF7B9F00DA0F2E /* MJExtensionConst.m in Sources */ = {isa = PBXBuildFile; fileRef = 2DE3CDCD1BEF7B9F00DA0F2E /* MJExtensionConst.m */; settings = {ASSET_TAGS = (); }; };
2120
2DE3CDE21BEF7B9F00DA0F2E /* MJFoundation.m in Sources */ = {isa = PBXBuildFile; fileRef = 2DE3CDCF1BEF7B9F00DA0F2E /* MJFoundation.m */; settings = {ASSET_TAGS = (); }; };
2221
2DE3CDE31BEF7B9F00DA0F2E /* MJProperty.m in Sources */ = {isa = PBXBuildFile; fileRef = 2DE3CDD11BEF7B9F00DA0F2E /* MJProperty.m */; settings = {ASSET_TAGS = (); }; };
@@ -75,8 +74,6 @@
7574
2DE3CDB61BEF7B3800DA0F2E /* MJExtensionExampleUITests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = MJExtensionExampleUITests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
7675
2DE3CDBA1BEF7B3800DA0F2E /* MJExtensionExampleUITests.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = MJExtensionExampleUITests.m; sourceTree = "<group>"; };
7776
2DE3CDBC1BEF7B3800DA0F2E /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
78-
2DE3CDC91BEF7B9F00DA0F2E /* MJDictionaryCache.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MJDictionaryCache.h; sourceTree = "<group>"; };
79-
2DE3CDCA1BEF7B9F00DA0F2E /* MJDictionaryCache.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = MJDictionaryCache.m; sourceTree = "<group>"; };
8077
2DE3CDCB1BEF7B9F00DA0F2E /* MJExtension.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MJExtension.h; sourceTree = "<group>"; };
8178
2DE3CDCC1BEF7B9F00DA0F2E /* MJExtensionConst.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MJExtensionConst.h; sourceTree = "<group>"; };
8279
2DE3CDCD1BEF7B9F00DA0F2E /* MJExtensionConst.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = MJExtensionConst.m; sourceTree = "<group>"; };
@@ -219,8 +216,6 @@
219216
2DE3CDC81BEF7B9F00DA0F2E /* MJExtension */ = {
220217
isa = PBXGroup;
221218
children = (
222-
2DE3CDC91BEF7B9F00DA0F2E /* MJDictionaryCache.h */,
223-
2DE3CDCA1BEF7B9F00DA0F2E /* MJDictionaryCache.m */,
224219
2DE3CDCB1BEF7B9F00DA0F2E /* MJExtension.h */,
225220
2DE3CDCC1BEF7B9F00DA0F2E /* MJExtensionConst.h */,
226221
2DE3CDCD1BEF7B9F00DA0F2E /* MJExtensionConst.m */,
@@ -449,7 +444,6 @@
449444
2DE3CDE21BEF7B9F00DA0F2E /* MJFoundation.m in Sources */,
450445
2DE3CDEA1BEF7B9F00DA0F2E /* NSString+MJExtension.m in Sources */,
451446
2DE3CD971BEF7B3800DA0F2E /* main.m in Sources */,
452-
2DE3CDE01BEF7B9F00DA0F2E /* MJDictionaryCache.m in Sources */,
453447
2D8FC6B81BEF7E89004471E9 /* MJExtensionConfig.m in Sources */,
454448
2DE3CDE41BEF7B9F00DA0F2E /* MJPropertyKey.m in Sources */,
455449
);

0 commit comments

Comments
 (0)