diff --git a/MJExtension.podspec b/MJExtension.podspec index b00dbb65..61ec3d4e 100644 --- a/MJExtension.podspec +++ b/MJExtension.podspec @@ -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" diff --git a/MJExtension/MJDictionaryCache.h b/MJExtension/MJDictionaryCache.h deleted file mode 100644 index 8d2b0151..00000000 --- a/MJExtension/MJDictionaryCache.h +++ /dev/null @@ -1,34 +0,0 @@ -// -// MJDictionaryCache.h -// MJExtensionExample -// -// Created by MJ Lee on 15/8/22. -// Copyright (c) 2015年 小码哥. All rights reserved. -// - -#import - -@interface MJDictionaryCache : NSObject -/** - * 缓存数据 - * - * @param dictId 字典标识 - * - * @return 缓存的字典 - */ -+ (id)setObject:(id)object forKey:(id)key forDictId:(const void *)dictId; - -/** - * 获得缓存的数据 - * - * @param dictId 字典标识 - */ -+ (id)objectForKey:(id)key forDictId:(const void *)dictId; - -/** - * 获得缓存的字典 - * - * @param dictId 字典标识 - */ -+ (id)dictWithDictId:(const void *)dictId; -@end diff --git a/MJExtension/MJDictionaryCache.m b/MJExtension/MJDictionaryCache.m deleted file mode 100644 index e12870a0..00000000 --- a/MJExtension/MJDictionaryCache.m +++ /dev/null @@ -1,37 +0,0 @@ -// -// MJDictionaryCache.m -// MJExtensionExample -// -// Created by MJ Lee on 15/8/22. -// Copyright (c) 2015年 小码哥. All rights reserved. -// - -#import "MJDictionaryCache.h" -#import - -@implementation MJDictionaryCache -+ (id)setObject:(id)object forKey:(id)key forDictId:(const void *)dictId -{ - // 获得字典 - NSMutableDictionary *dict = [self dictWithDictId:dictId]; - if (dict == nil) { - dict = [NSMutableDictionary dictionary]; - objc_setAssociatedObject(self, dictId, dict, OBJC_ASSOCIATION_RETAIN_NONATOMIC); - } - - // 存储数据 - dict[key] = object; - - return dict; -} - -+ (id)objectForKey:(id)key forDictId:(const void *)dictId -{ - return [self dictWithDictId:dictId][key]; -} - -+ (id)dictWithDictId:(const void *)dictId -{ - return objc_getAssociatedObject(self, dictId); -} -@end \ No newline at end of file diff --git a/MJExtension/MJPropertyType.m b/MJExtension/MJPropertyType.m index 1fd04884..b78b2028 100755 --- a/MJExtension/MJPropertyType.m +++ b/MJExtension/MJPropertyType.m @@ -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; } diff --git a/MJExtension/NSObject+MJClass.m b/MJExtension/NSObject+MJClass.m index 0320cb69..789b9a89 100644 --- a/MJExtension/NSObject+MJClass.m +++ b/MJExtension/NSObject+MJClass.m @@ -11,15 +11,36 @@ #import "NSObject+MJKeyValue.h" #import "MJFoundation.h" #import -#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就直接返回 @@ -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 diff --git a/MJExtension/NSObject+MJKeyValue.m b/MJExtension/NSObject+MJKeyValue.m index 54184fed..5635e292 100755 --- a/MJExtension/NSObject+MJKeyValue.m +++ b/MJExtension/NSObject+MJKeyValue.m @@ -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]; diff --git a/MJExtension/NSObject+MJProperty.m b/MJExtension/NSObject+MJProperty.m index 22280a77..9ac6134c 100644 --- a/MJExtension/NSObject+MJProperty.m +++ b/MJExtension/NSObject+MJProperty.m @@ -13,14 +13,11 @@ #import "MJProperty.h" #import "MJFoundation.h" #import -#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'; @@ -28,6 +25,33 @@ @implementation NSObject (Property) 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 { @@ -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]; @@ -154,7 +178,7 @@ + (NSMutableArray *)properties free(properties); }]; - [MJDictionaryCache setObject:cachedProperties forKey:NSStringFromClass(self) forDictId:&MJCachedPropertiesKey]; + [self dictForKey:&MJCachedPropertiesKey][NSStringFromClass(self)] = cachedProperties; } return cachedProperties; @@ -193,7 +217,7 @@ + (void)mj_setupObjectClassInArray:(MJObjectClassInArray)objectClassInArray { [self mj_setupBlockReturnValue:objectClassInArray key:&MJObjectClassInArrayKey]; - [[MJDictionaryCache dictWithDictId:&MJCachedPropertiesKey] removeAllObjects]; + [[self dictForKey:&MJCachedPropertiesKey] removeAllObjects]; } #pragma mark - key配置 @@ -201,14 +225,14 @@ + (void)mj_setupReplacedKeyFromPropertyName:(MJReplacedKeyFromPropertyName)repla { [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 diff --git a/MJExtensionExample.xcodeproj/project.pbxproj b/MJExtensionExample.xcodeproj/project.pbxproj index b52813b4..43f001d5 100644 --- a/MJExtensionExample.xcodeproj/project.pbxproj +++ b/MJExtensionExample.xcodeproj/project.pbxproj @@ -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 = (); }; }; @@ -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 = ""; }; 2DE3CDBC1BEF7B3800DA0F2E /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; - 2DE3CDC91BEF7B9F00DA0F2E /* MJDictionaryCache.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MJDictionaryCache.h; sourceTree = ""; }; - 2DE3CDCA1BEF7B9F00DA0F2E /* MJDictionaryCache.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = MJDictionaryCache.m; sourceTree = ""; }; 2DE3CDCB1BEF7B9F00DA0F2E /* MJExtension.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MJExtension.h; sourceTree = ""; }; 2DE3CDCC1BEF7B9F00DA0F2E /* MJExtensionConst.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MJExtensionConst.h; sourceTree = ""; }; 2DE3CDCD1BEF7B9F00DA0F2E /* MJExtensionConst.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = MJExtensionConst.m; sourceTree = ""; }; @@ -219,8 +216,6 @@ 2DE3CDC81BEF7B9F00DA0F2E /* MJExtension */ = { isa = PBXGroup; children = ( - 2DE3CDC91BEF7B9F00DA0F2E /* MJDictionaryCache.h */, - 2DE3CDCA1BEF7B9F00DA0F2E /* MJDictionaryCache.m */, 2DE3CDCB1BEF7B9F00DA0F2E /* MJExtension.h */, 2DE3CDCC1BEF7B9F00DA0F2E /* MJExtensionConst.h */, 2DE3CDCD1BEF7B9F00DA0F2E /* MJExtensionConst.m */, @@ -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 */, );