Skip to content

Commit 1b0f763

Browse files
committed
Add NSError to MJFoundation
Add NSError to MJFoundation
1 parent 276adf8 commit 1b0f763

File tree

12 files changed

+114
-58
lines changed

12 files changed

+114
-58
lines changed

MJExtension/MJDictionaryCache.h

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
//
2+
// MJDictionaryCache.h
3+
// MJExtensionExample
4+
//
5+
// Created by MJ Lee on 15/8/22.
6+
// Copyright (c) 2015年 小码哥. All rights reserved.
7+
//
8+
9+
#import <Foundation/Foundation.h>
10+
11+
@interface MJDictionaryCache : NSObject
12+
/**
13+
* 缓存数据
14+
*
15+
* @param dictId 字典标识
16+
*
17+
* @return 缓存的字典
18+
*/
19+
+ (id)setObject:(id)object forKey:(id<NSCopying>)key forDictId:(const void *)dictId;
20+
21+
/**
22+
* 获得缓存的数据
23+
*
24+
* @param dictId 字典标识
25+
*/
26+
+ (id)objectForKey:(id<NSCopying>)key forDictId:(const void *)dictId;
27+
28+
/**
29+
* 获得缓存的字典
30+
*
31+
* @param dictId 字典标识
32+
*/
33+
+ (id)dictWithDictId:(const void *)dictId;
34+
@end

MJExtension/MJDictionaryCache.m

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
//
2+
// MJDictionaryCache.m
3+
// MJExtensionExample
4+
//
5+
// Created by MJ Lee on 15/8/22.
6+
// Copyright (c) 2015年 小码哥. All rights reserved.
7+
//
8+
9+
#import "MJDictionaryCache.h"
10+
#import <objc/runtime.h>
11+
12+
@implementation MJDictionaryCache
13+
+ (id)setObject:(id)object forKey:(id<NSCopying>)key forDictId:(const void *)dictId
14+
{
15+
// 获得字典
16+
NSMutableDictionary *dict = [self dictWithDictId:dictId];
17+
if (dict == nil) {
18+
dict = [NSMutableDictionary dictionary];
19+
objc_setAssociatedObject(self, dictId, dict, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
20+
}
21+
22+
// 存储数据
23+
dict[key] = object;
24+
25+
return dict;
26+
}
27+
28+
+ (id)objectForKey:(id<NSCopying>)key forDictId:(const void *)dictId
29+
{
30+
return [self dictWithDictId:dictId][key];
31+
}
32+
33+
+ (id)dictWithDictId:(const void *)dictId
34+
{
35+
return objc_getAssociatedObject(self, dictId);
36+
}
37+
@end

MJExtension/MJFoundation.m

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ + (NSSet *)foundationClasses
2323
[NSDate class],
2424
[NSValue class],
2525
[NSData class],
26+
[NSError class],
2627
[NSArray class],
2728
[NSDictionary class],
2829
[NSString class],

MJExtension/MJProperty.m

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,6 @@ @interface MJProperty()
1717

1818
@implementation MJProperty
1919

20-
/** originKey作为key,propertyKeys作为value */
21-
static NSMutableDictionary *originKeyPropertyKey_;
22-
+ (void)load
23-
{
24-
originKeyPropertyKey_ = [NSMutableDictionary dictionary];
25-
}
26-
2720
#pragma mark - 懒加载
2821
- (NSMutableDictionary *)propertyKeysDict
2922
{

MJExtension/MJPropertyType.m

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

1415
@implementation MJPropertyType
1516

16-
#pragma mark - 缓存
17-
static NSMutableDictionary *cachedTypes_;
18-
+ (void)load
19-
{
20-
cachedTypes_ = [NSMutableDictionary dictionary];
21-
}
22-
2317
+ (instancetype)cachedTypeWithCode:(NSString *)code
2418
{
2519
MJExtensionAssertParamNotNil2(code, nil);
2620

27-
MJPropertyType *type = cachedTypes_[code];
21+
static const char MJCachedTypesKey = '\0';
22+
23+
MJPropertyType *type = [MJDictionaryCache objectForKey:code forDictId:&MJCachedTypesKey];
2824
if (type == nil) {
2925
type = [[self alloc] init];
3026
type.code = code;
31-
cachedTypes_[code] = type;
27+
[MJDictionaryCache setObject:type forKey:code forDictId:&MJCachedTypesKey];
3228
}
3329
return type;
3430
}

MJExtension/NSObject+MJClass.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,5 +86,5 @@ typedef NSArray * (^MJIgnoredCodingPropertyNames)();
8686
+ (NSMutableArray *)totalIgnoredCodingPropertyNames;
8787

8888
#pragma mark - 内部使用
89-
+ (void)setupBlockReturnValue:(id (^)())block key:(const char *)key dict:(NSMutableDictionary *)dict;
89+
+ (void)setupBlockReturnValue:(id (^)())block key:(const char *)key;
9090
@end

MJExtension/NSObject+MJClass.m

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

1516
static const char MJAllowedPropertyNamesKey = '\0';
1617
static const char MJIgnoredPropertyNamesKey = '\0';
@@ -19,18 +20,6 @@
1920

2021
@implementation NSObject (MJClass)
2122

22-
static NSMutableDictionary *allowedPropertyNames_;
23-
static NSMutableDictionary *ignoredPropertyNames_;
24-
static NSMutableDictionary *allowedCodingPropertyNames_;
25-
static NSMutableDictionary *ignoredCodingPropertyNames_;
26-
+ (void)load
27-
{
28-
allowedPropertyNames_ = [NSMutableDictionary dictionary];
29-
ignoredPropertyNames_ = [NSMutableDictionary dictionary];
30-
allowedCodingPropertyNames_ = [NSMutableDictionary dictionary];
31-
ignoredCodingPropertyNames_ = [NSMutableDictionary dictionary];
32-
}
33-
3423
+ (void)enumerateClasses:(MJClassesEnumeration)enumeration
3524
{
3625
// 1.没有block就直接返回
@@ -78,48 +67,48 @@ + (void)enumerateAllClasses:(MJClassesEnumeration)enumeration
7867
#pragma mark - 属性黑名单配置
7968
+ (void)setupIgnoredPropertyNames:(MJIgnoredPropertyNames)ignoredPropertyNames
8069
{
81-
[self setupBlockReturnValue:ignoredPropertyNames key:&MJIgnoredPropertyNamesKey dict:ignoredPropertyNames_];
70+
[self setupBlockReturnValue:ignoredPropertyNames key:&MJIgnoredPropertyNamesKey];
8271
}
8372

8473
+ (NSMutableArray *)totalIgnoredPropertyNames
8574
{
86-
return [self totalObjectsWithSelector:@selector(ignoredPropertyNames) key:&MJIgnoredPropertyNamesKey dict:ignoredPropertyNames_];
75+
return [self totalObjectsWithSelector:@selector(ignoredPropertyNames) key:&MJIgnoredPropertyNamesKey];
8776
}
8877

8978
#pragma mark - 归档属性黑名单配置
9079
+ (void)setupIgnoredCodingPropertyNames:(MJIgnoredCodingPropertyNames)ignoredCodingPropertyNames
9180
{
92-
[self setupBlockReturnValue:ignoredCodingPropertyNames key:&MJIgnoredCodingPropertyNamesKey dict:ignoredCodingPropertyNames_];
81+
[self setupBlockReturnValue:ignoredCodingPropertyNames key:&MJIgnoredCodingPropertyNamesKey];
9382
}
9483

9584
+ (NSMutableArray *)totalIgnoredCodingPropertyNames
9685
{
97-
return [self totalObjectsWithSelector:@selector(ignoredCodingPropertyNames) key:&MJIgnoredCodingPropertyNamesKey dict:ignoredCodingPropertyNames_];
86+
return [self totalObjectsWithSelector:@selector(ignoredCodingPropertyNames) key:&MJIgnoredCodingPropertyNamesKey];
9887
}
9988

10089
#pragma mark - 属性白名单配置
10190
+ (void)setupAllowedPropertyNames:(MJAllowedPropertyNames)allowedPropertyNames;
10291
{
103-
[self setupBlockReturnValue:allowedPropertyNames key:&MJAllowedPropertyNamesKey dict:allowedPropertyNames_];
92+
[self setupBlockReturnValue:allowedPropertyNames key:&MJAllowedPropertyNamesKey];
10493
}
10594

10695
+ (NSMutableArray *)totalAllowedPropertyNames
10796
{
108-
return [self totalObjectsWithSelector:@selector(allowedPropertyNames) key:&MJAllowedPropertyNamesKey dict:allowedPropertyNames_];
97+
return [self totalObjectsWithSelector:@selector(allowedPropertyNames) key:&MJAllowedPropertyNamesKey];
10998
}
11099

111100
#pragma mark - 归档属性白名单配置
112101
+ (void)setupAllowedCodingPropertyNames:(MJAllowedCodingPropertyNames)allowedCodingPropertyNames
113102
{
114-
[self setupBlockReturnValue:allowedCodingPropertyNames key:&MJAllowedCodingPropertyNamesKey dict:allowedCodingPropertyNames_];
103+
[self setupBlockReturnValue:allowedCodingPropertyNames key:&MJAllowedCodingPropertyNamesKey];
115104
}
116105

117106
+ (NSMutableArray *)totalAllowedCodingPropertyNames
118107
{
119-
return [self totalObjectsWithSelector:@selector(allowedCodingPropertyNames) key:&MJAllowedCodingPropertyNamesKey dict:allowedCodingPropertyNames_];
108+
return [self totalObjectsWithSelector:@selector(allowedCodingPropertyNames) key:&MJAllowedCodingPropertyNamesKey];
120109
}
121110
#pragma mark - block和方法处理:存储block的返回值
122-
+ (void)setupBlockReturnValue:(id (^)())block key:(const char *)key dict:(NSMutableDictionary *)dict
111+
+ (void)setupBlockReturnValue:(id (^)())block key:(const char *)key
123112
{
124113
if (block) {
125114
objc_setAssociatedObject(self, key, block(), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
@@ -128,16 +117,16 @@ + (void)setupBlockReturnValue:(id (^)())block key:(const char *)key dict:(NSMuta
128117
}
129118

130119
// 清空数据
131-
[dict removeAllObjects];
120+
[[MJDictionaryCache dictWithDictId:key] removeAllObjects];
132121
}
133122

134-
+ (NSMutableArray *)totalObjectsWithSelector:(SEL)selector key:(const char *)key dict:(NSMutableDictionary *)dict
123+
+ (NSMutableArray *)totalObjectsWithSelector:(SEL)selector key:(const char *)key
135124
{
136-
NSMutableArray *array = dict[NSStringFromClass(self)];
125+
NSMutableArray *array = [MJDictionaryCache objectForKey:NSStringFromClass(self) forDictId:key];
137126
if (array) return array;
138127

139128
// 创建、存储
140-
dict[NSStringFromClass(self)] = array = [NSMutableArray array];
129+
[MJDictionaryCache setObject:array = [NSMutableArray array] forKey:NSStringFromClass(self) forDictId:key];
141130

142131
if ([self respondsToSelector:selector]) {
143132
#pragma clang diagnostic push

MJExtension/NSObject+MJKeyValue.m

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,10 @@ + (BOOL)isReferenceReplacedKeyWhenCreatingKeyValues
4141
}
4242

4343
#pragma mark - --常用的对象--
44-
static NSNumberFormatter *_numberFormatter;
44+
static NSNumberFormatter *numberFormatter_;
4545
+ (void)load
4646
{
47-
_numberFormatter = [[NSNumberFormatter alloc] init];
47+
numberFormatter_ = [[NSNumberFormatter alloc] init];
4848
}
4949

5050
#pragma mark - --公共方法--
@@ -139,7 +139,7 @@ - (instancetype)setKeyValues:(id)keyValues context:(NSManagedObjectContext *)con
139139
NSString *oldValue = value;
140140

141141
// NSString -> NSNumber
142-
value = [_numberFormatter numberFromString:oldValue];
142+
value = [numberFormatter_ numberFromString:oldValue];
143143

144144
// 如果是BOOL
145145
if (type.isBoolType) {

MJExtension/NSObject+MJProperty.m

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#import "MJProperty.h"
1414
#import "MJFoundation.h"
1515
#import <objc/runtime.h>
16+
#import "MJDictionaryCache.h"
1617

1718
@implementation NSObject (Property)
1819

@@ -21,11 +22,7 @@ @implementation NSObject (Property)
2122
static const char MJNewValueFromOldValueKey = '\0';
2223
static const char MJObjectClassInArrayKey = '\0';
2324

24-
static NSMutableDictionary *cachedProperties_;
25-
+ (void)load
26-
{
27-
cachedProperties_ = [NSMutableDictionary dictionary];
28-
}
25+
static const char MJCachedPropertiesKey = '\0';
2926

3027
#pragma mark - --私有方法--
3128
+ (NSString *)propertyKey:(NSString *)propertyName
@@ -117,7 +114,7 @@ + (NSMutableArray *)properties
117114
//***objc_getAssociatedObject 方法用于判断当前是否已经获取过MJCachedPropertiesKey对应的关联对象
118115
// 1> 关联到的对象
119116
// 2> 关联的属性 key
120-
NSMutableArray *cachedProperties = cachedProperties_[NSStringFromClass(self)];
117+
NSMutableArray *cachedProperties = [MJDictionaryCache objectForKey:NSStringFromClass(self) forDictId:&MJCachedPropertiesKey];
121118

122119
//***
123120
if (cachedProperties == nil) {
@@ -150,7 +147,7 @@ + (NSMutableArray *)properties
150147
}];
151148

152149
//*** 在此时设置当前这个类为关联对象,这样下次就不会重复获取类的相关属性。
153-
cachedProperties_[NSStringFromClass(self)] = cachedProperties;
150+
[MJDictionaryCache setObject:cachedProperties forKey:NSStringFromClass(self) forDictId:&MJCachedPropertiesKey];
154151
//***
155152
}
156153

@@ -184,20 +181,23 @@ + (id)getNewValueFromObject:(__weak id)object oldValue:(__weak id)oldValue prope
184181
#pragma mark - array model class配置
185182
+ (void)setupObjectClassInArray:(MJObjectClassInArray)objectClassInArray
186183
{
187-
[self setupBlockReturnValue:objectClassInArray key:&MJObjectClassInArrayKey dict:nil];
188-
[cachedProperties_ removeAllObjects];
184+
[self setupBlockReturnValue:objectClassInArray key:&MJObjectClassInArrayKey];
185+
186+
[[MJDictionaryCache dictWithDictId:&MJCachedPropertiesKey] removeAllObjects];
189187
}
190188

191189
#pragma mark - key配置
192190
+ (void)setupReplacedKeyFromPropertyName:(MJReplacedKeyFromPropertyName)replacedKeyFromPropertyName
193191
{
194-
[self setupBlockReturnValue:replacedKeyFromPropertyName key:&MJReplacedKeyFromPropertyNameKey dict:nil];
195-
[cachedProperties_ removeAllObjects];
192+
[self setupBlockReturnValue:replacedKeyFromPropertyName key:&MJReplacedKeyFromPropertyNameKey];
193+
194+
[[MJDictionaryCache dictWithDictId:&MJCachedPropertiesKey] removeAllObjects];
196195
}
197196

198197
+ (void)setupReplacedKeyFromPropertyName121:(MJReplacedKeyFromPropertyName121)replacedKeyFromPropertyName121
199198
{
200199
objc_setAssociatedObject(self, &MJReplacedKeyFromPropertyName121Key, replacedKeyFromPropertyName121, OBJC_ASSOCIATION_COPY_NONATOMIC);
201-
[cachedProperties_ removeAllObjects];
200+
201+
[[MJDictionaryCache dictWithDictId:&MJCachedPropertiesKey] removeAllObjects];
202202
}
203203
@end

MJExtension/NSString+MJExtension.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,6 @@ - (BOOL)isPureInt
7171

7272
- (NSURL *)url
7373
{
74-
return [NSURL URLWithString:(NSString *)CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault, (CFStringRef)self,(CFStringRef)@"!$&'()*+,-./:;=?@_~%#[]", NULL,kCFStringEncodingUTF8))];
74+
return [NSURL URLWithString:(NSString *)CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault, (CFStringRef)self, (CFStringRef)@"!$&'()*+,-./:;=?@_~%#[]", NULL,kCFStringEncodingUTF8))];
7575
}
7676
@end

MJExtensionExample/MJExtensionExample.xcodeproj/project.pbxproj

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
2D35374C1B24868D00D5B7A8 /* Dog.m in Sources */ = {isa = PBXBuildFile; fileRef = 2D35374B1B24868D00D5B7A8 /* Dog.m */; };
1313
2D3537551B248D9B00D5B7A8 /* Book.m in Sources */ = {isa = PBXBuildFile; fileRef = 2D3537541B248D9B00D5B7A8 /* Book.m */; };
1414
2D3576231A7913F30056D6BF /* Bag.m in Sources */ = {isa = PBXBuildFile; fileRef = 2D3576221A7913F30056D6BF /* Bag.m */; };
15+
2D3E79571B878DFE003A2B27 /* MJDictionaryCache.m in Sources */ = {isa = PBXBuildFile; fileRef = 2D3E79561B878DFD003A2B27 /* MJDictionaryCache.m */; };
1516
2D49F3211B2855EC008D5A72 /* Box.m in Sources */ = {isa = PBXBuildFile; fileRef = 2D49F3201B2855EC008D5A72 /* Box.m */; };
1617
2D4A0BF91B25615700D323A7 /* MJFoundation.m in Sources */ = {isa = PBXBuildFile; fileRef = 2D4A0BEB1B25615600D323A7 /* MJFoundation.m */; };
1718
2D4A0BFA1B25615700D323A7 /* MJProperty.m in Sources */ = {isa = PBXBuildFile; fileRef = 2D4A0BED1B25615600D323A7 /* MJProperty.m */; };
@@ -59,6 +60,8 @@
5960
2D3537541B248D9B00D5B7A8 /* Book.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = Book.m; sourceTree = "<group>"; };
6061
2D3576211A7913F30056D6BF /* Bag.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Bag.h; sourceTree = "<group>"; };
6162
2D3576221A7913F30056D6BF /* Bag.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = Bag.m; sourceTree = "<group>"; };
63+
2D3E79551B878DFD003A2B27 /* MJDictionaryCache.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MJDictionaryCache.h; sourceTree = "<group>"; };
64+
2D3E79561B878DFD003A2B27 /* MJDictionaryCache.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = MJDictionaryCache.m; sourceTree = "<group>"; };
6265
2D49F31F1B2855EC008D5A72 /* Box.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Box.h; sourceTree = "<group>"; };
6366
2D49F3201B2855EC008D5A72 /* Box.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = Box.m; sourceTree = "<group>"; };
6467
2D4A0BE91B25615600D323A7 /* MJExtension.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MJExtension.h; sourceTree = "<group>"; };
@@ -121,6 +124,8 @@
121124
2D4A0BE91B25615600D323A7 /* MJExtension.h */,
122125
2D4A0BEA1B25615600D323A7 /* MJFoundation.h */,
123126
2D4A0BEB1B25615600D323A7 /* MJFoundation.m */,
127+
2D3E79551B878DFD003A2B27 /* MJDictionaryCache.h */,
128+
2D3E79561B878DFD003A2B27 /* MJDictionaryCache.m */,
124129
2D4A0BEC1B25615600D323A7 /* MJProperty.h */,
125130
2D4A0BED1B25615600D323A7 /* MJProperty.m */,
126131
2D581C7C1B7A2912006ACCEB /* MJPropertyKey.h */,
@@ -288,6 +293,7 @@
288293
2D4A0BFE1B25615700D323A7 /* NSObject+MJProperty.m in Sources */,
289294
2D581C841B7A291A006ACCEB /* NSObject+MJClass.m in Sources */,
290295
2D306F5D1B58A6650069910E /* MJExtensionConst.m in Sources */,
296+
2D3E79571B878DFE003A2B27 /* MJDictionaryCache.m in Sources */,
291297
2D35374C1B24868D00D5B7A8 /* Dog.m in Sources */,
292298
2D3576231A7913F30056D6BF /* Bag.m in Sources */,
293299
2D4A0BFC1B25615700D323A7 /* NSObject+MJCoding.m in Sources */,

0 commit comments

Comments
 (0)