Skip to content

Commit

Permalink
修复NSNumber相关问题、字典报错问题
Browse files Browse the repository at this point in the history
  • Loading branch information
CoderMJLee committed Jan 15, 2016
1 parent 69e11e4 commit 92fc691
Show file tree
Hide file tree
Showing 8 changed files with 67 additions and 96 deletions.
2 changes: 1 addition & 1 deletion MJExtension.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = "MJExtension"
s.version = "3.0.9"
s.version = "3.0.10"
s.ios.deployment_target = '6.0'
s.osx.deployment_target = '10.8'
s.summary = "A fast and convenient conversion between JSON and model"
Expand Down
34 changes: 0 additions & 34 deletions MJExtension/MJDictionaryCache.h

This file was deleted.

37 changes: 0 additions & 37 deletions MJExtension/MJDictionaryCache.m

This file was deleted.

13 changes: 8 additions & 5 deletions MJExtension/MJPropertyType.m
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,24 @@
#import "MJExtension.h"
#import "MJFoundation.h"
#import "MJExtensionConst.h"
#import "MJDictionaryCache.h"

@implementation MJPropertyType

static NSMutableDictionary *types_;
+ (void)initialize
{
types_ = [NSMutableDictionary dictionary];
}

+ (instancetype)cachedTypeWithCode:(NSString *)code
{
MJExtensionAssertParamNotNil2(code, nil);

static const char MJCachedTypesKey = '\0';

MJPropertyType *type = [MJDictionaryCache objectForKey:code forDictId:&MJCachedTypesKey];
MJPropertyType *type = types_[code];
if (type == nil) {
type = [[self alloc] init];
type.code = code;
[MJDictionaryCache setObject:type forKey:code forDictId:&MJCachedTypesKey];
types_[code] = type;
}
return type;
}
Expand Down
29 changes: 25 additions & 4 deletions MJExtension/NSObject+MJClass.m
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,36 @@
#import "NSObject+MJKeyValue.h"
#import "MJFoundation.h"
#import <objc/runtime.h>
#import "MJDictionaryCache.h"

static const char MJAllowedPropertyNamesKey = '\0';
static const char MJIgnoredPropertyNamesKey = '\0';
static const char MJAllowedCodingPropertyNamesKey = '\0';
static const char MJIgnoredCodingPropertyNamesKey = '\0';

static NSMutableDictionary *allowedPropertyNamesDict_;
static NSMutableDictionary *ignoredPropertyNamesDict_;
static NSMutableDictionary *allowedCodingPropertyNamesDict_;
static NSMutableDictionary *ignoredCodingPropertyNamesDict_;

@implementation NSObject (MJClass)

+ (void)load
{
allowedPropertyNamesDict_ = [NSMutableDictionary dictionary];
ignoredPropertyNamesDict_ = [NSMutableDictionary dictionary];
allowedCodingPropertyNamesDict_ = [NSMutableDictionary dictionary];
ignoredCodingPropertyNamesDict_ = [NSMutableDictionary dictionary];
}

+ (NSMutableDictionary *)dictForKey:(const void *)key
{
if (key == &MJAllowedPropertyNamesKey) return allowedPropertyNamesDict_;
if (key == &MJIgnoredPropertyNamesKey) return ignoredPropertyNamesDict_;
if (key == &MJAllowedCodingPropertyNamesKey) return allowedCodingPropertyNamesDict_;
if (key == &MJIgnoredCodingPropertyNamesKey) return ignoredCodingPropertyNamesDict_;
return nil;
}

+ (void)mj_enumerateClasses:(MJClassesEnumeration)enumeration
{
// 1.没有block就直接返回
Expand Down Expand Up @@ -117,16 +138,16 @@ + (void)mj_setupBlockReturnValue:(id (^)())block key:(const char *)key
}

// 清空数据
[[MJDictionaryCache dictWithDictId:key] removeAllObjects];
[[self dictForKey:key] removeAllObjects];
}

+ (NSMutableArray *)mj_totalObjectsWithSelector:(SEL)selector key:(const char *)key
{
NSMutableArray *array = [MJDictionaryCache objectForKey:NSStringFromClass(self) forDictId:key];
NSMutableArray *array = [self dictForKey:key][NSStringFromClass(self)];
if (array) return array;

// 创建、存储
[MJDictionaryCache setObject:array = [NSMutableArray array] forKey:NSStringFromClass(self) forDictId:key];
[self dictForKey:key][NSStringFromClass(self)] = array = [NSMutableArray array];

if ([self respondsToSelector:selector]) {
#pragma clang diagnostic push
Expand Down
2 changes: 1 addition & 1 deletion MJExtension/NSObject+MJKeyValue.m
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ - (instancetype)mj_setKeyValues:(id)keyValues context:(NSManagedObjectContext *)
NSString *oldValue = value;

// NSString -> NSNumber
if (type.class == [NSDecimalNumber class]) {
if (type.typeClass == [NSDecimalNumber class]) {
value = [NSDecimalNumber decimalNumberWithString:oldValue];
} else {
value = [numberFormatter_ numberFromString:oldValue];
Expand Down
40 changes: 32 additions & 8 deletions MJExtension/NSObject+MJProperty.m
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,45 @@
#import "MJProperty.h"
#import "MJFoundation.h"
#import <objc/runtime.h>
#import "MJDictionaryCache.h"

#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wundeclared-selector"
#pragma clang diagnostic ignored "-Warc-performSelector-leaks"

@implementation NSObject (Property)

static const char MJReplacedKeyFromPropertyNameKey = '\0';
static const char MJReplacedKeyFromPropertyName121Key = '\0';
static const char MJNewValueFromOldValueKey = '\0';
static const char MJObjectClassInArrayKey = '\0';

static const char MJCachedPropertiesKey = '\0';

@implementation NSObject (Property)

static NSMutableDictionary *replacedKeyFromPropertyNameDict_;
static NSMutableDictionary *replacedKeyFromPropertyName121Dict_;
static NSMutableDictionary *newValueFromOldValueDict_;
static NSMutableDictionary *objectClassInArrayDict_;
static NSMutableDictionary *cachedPropertiesDict_;

+ (void)load
{
replacedKeyFromPropertyNameDict_ = [NSMutableDictionary dictionary];
replacedKeyFromPropertyName121Dict_ = [NSMutableDictionary dictionary];
newValueFromOldValueDict_ = [NSMutableDictionary dictionary];
objectClassInArrayDict_ = [NSMutableDictionary dictionary];
cachedPropertiesDict_ = [NSMutableDictionary dictionary];
}

+ (NSMutableDictionary *)dictForKey:(const void *)key
{
if (key == &MJReplacedKeyFromPropertyNameKey) return replacedKeyFromPropertyNameDict_;
if (key == &MJReplacedKeyFromPropertyName121Key) return replacedKeyFromPropertyName121Dict_;
if (key == &MJNewValueFromOldValueKey) return newValueFromOldValueDict_;
if (key == &MJObjectClassInArrayKey) return objectClassInArrayDict_;
if (key == &MJCachedPropertiesKey) return cachedPropertiesDict_;
return nil;
}

#pragma mark - --私有方法--
+ (NSString *)propertyKey:(NSString *)propertyName
{
Expand Down Expand Up @@ -124,7 +148,7 @@ + (void)mj_enumerateProperties:(MJPropertiesEnumeration)enumeration
#pragma mark - 公共方法
+ (NSMutableArray *)properties
{
NSMutableArray *cachedProperties = [MJDictionaryCache objectForKey:NSStringFromClass(self) forDictId:&MJCachedPropertiesKey];
NSMutableArray *cachedProperties = [self dictForKey:&MJCachedPropertiesKey][NSStringFromClass(self)];

if (cachedProperties == nil) {
cachedProperties = [NSMutableArray array];
Expand Down Expand Up @@ -154,7 +178,7 @@ + (NSMutableArray *)properties
free(properties);
}];

[MJDictionaryCache setObject:cachedProperties forKey:NSStringFromClass(self) forDictId:&MJCachedPropertiesKey];
[self dictForKey:&MJCachedPropertiesKey][NSStringFromClass(self)] = cachedProperties;
}

return cachedProperties;
Expand Down Expand Up @@ -193,22 +217,22 @@ + (void)mj_setupObjectClassInArray:(MJObjectClassInArray)objectClassInArray
{
[self mj_setupBlockReturnValue:objectClassInArray key:&MJObjectClassInArrayKey];

[[MJDictionaryCache dictWithDictId:&MJCachedPropertiesKey] removeAllObjects];
[[self dictForKey:&MJCachedPropertiesKey] removeAllObjects];
}

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

[[MJDictionaryCache dictWithDictId:&MJCachedPropertiesKey] removeAllObjects];
[[self dictForKey:&MJCachedPropertiesKey] removeAllObjects];
}

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

[[MJDictionaryCache dictWithDictId:&MJCachedPropertiesKey] removeAllObjects];
[[self dictForKey:&MJCachedPropertiesKey] removeAllObjects];
}
@end

Expand Down
6 changes: 0 additions & 6 deletions MJExtensionExample.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
2DE3CDA51BEF7B3800DA0F2E /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 2DE3CDA31BEF7B3800DA0F2E /* LaunchScreen.storyboard */; };
2DE3CDB01BEF7B3800DA0F2E /* MJExtensionExampleTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 2DE3CDAF1BEF7B3800DA0F2E /* MJExtensionExampleTests.m */; };
2DE3CDBB1BEF7B3800DA0F2E /* MJExtensionExampleUITests.m in Sources */ = {isa = PBXBuildFile; fileRef = 2DE3CDBA1BEF7B3800DA0F2E /* MJExtensionExampleUITests.m */; };
2DE3CDE01BEF7B9F00DA0F2E /* MJDictionaryCache.m in Sources */ = {isa = PBXBuildFile; fileRef = 2DE3CDCA1BEF7B9F00DA0F2E /* MJDictionaryCache.m */; settings = {ASSET_TAGS = (); }; };
2DE3CDE11BEF7B9F00DA0F2E /* MJExtensionConst.m in Sources */ = {isa = PBXBuildFile; fileRef = 2DE3CDCD1BEF7B9F00DA0F2E /* MJExtensionConst.m */; settings = {ASSET_TAGS = (); }; };
2DE3CDE21BEF7B9F00DA0F2E /* MJFoundation.m in Sources */ = {isa = PBXBuildFile; fileRef = 2DE3CDCF1BEF7B9F00DA0F2E /* MJFoundation.m */; settings = {ASSET_TAGS = (); }; };
2DE3CDE31BEF7B9F00DA0F2E /* MJProperty.m in Sources */ = {isa = PBXBuildFile; fileRef = 2DE3CDD11BEF7B9F00DA0F2E /* MJProperty.m */; settings = {ASSET_TAGS = (); }; };
Expand Down Expand Up @@ -75,8 +74,6 @@
2DE3CDB61BEF7B3800DA0F2E /* MJExtensionExampleUITests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = MJExtensionExampleUITests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
2DE3CDBA1BEF7B3800DA0F2E /* MJExtensionExampleUITests.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = MJExtensionExampleUITests.m; sourceTree = "<group>"; };
2DE3CDBC1BEF7B3800DA0F2E /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
2DE3CDC91BEF7B9F00DA0F2E /* MJDictionaryCache.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MJDictionaryCache.h; sourceTree = "<group>"; };
2DE3CDCA1BEF7B9F00DA0F2E /* MJDictionaryCache.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = MJDictionaryCache.m; sourceTree = "<group>"; };
2DE3CDCB1BEF7B9F00DA0F2E /* MJExtension.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MJExtension.h; sourceTree = "<group>"; };
2DE3CDCC1BEF7B9F00DA0F2E /* MJExtensionConst.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MJExtensionConst.h; sourceTree = "<group>"; };
2DE3CDCD1BEF7B9F00DA0F2E /* MJExtensionConst.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = MJExtensionConst.m; sourceTree = "<group>"; };
Expand Down Expand Up @@ -219,8 +216,6 @@
2DE3CDC81BEF7B9F00DA0F2E /* MJExtension */ = {
isa = PBXGroup;
children = (
2DE3CDC91BEF7B9F00DA0F2E /* MJDictionaryCache.h */,
2DE3CDCA1BEF7B9F00DA0F2E /* MJDictionaryCache.m */,
2DE3CDCB1BEF7B9F00DA0F2E /* MJExtension.h */,
2DE3CDCC1BEF7B9F00DA0F2E /* MJExtensionConst.h */,
2DE3CDCD1BEF7B9F00DA0F2E /* MJExtensionConst.m */,
Expand Down Expand Up @@ -449,7 +444,6 @@
2DE3CDE21BEF7B9F00DA0F2E /* MJFoundation.m in Sources */,
2DE3CDEA1BEF7B9F00DA0F2E /* NSString+MJExtension.m in Sources */,
2DE3CD971BEF7B3800DA0F2E /* main.m in Sources */,
2DE3CDE01BEF7B9F00DA0F2E /* MJDictionaryCache.m in Sources */,
2D8FC6B81BEF7E89004471E9 /* MJExtensionConfig.m in Sources */,
2DE3CDE41BEF7B9F00DA0F2E /* MJPropertyKey.m in Sources */,
);
Expand Down

0 comments on commit 92fc691

Please sign in to comment.