15
15
16
16
@implementation NSObject (MJKeyValue)
17
17
18
+ static const char MJIgnoreReplacedKeyWhenGettingKeyValuesKey = ' \0 ' ;
19
+
20
+ - (void )setIgnoreReplacedKeyWhenGettingKeyValues : (BOOL )ignoreReplacedKeyWhenGettingKeyValues
21
+ {
22
+ objc_setAssociatedObject (self, &MJIgnoreReplacedKeyWhenGettingKeyValuesKey, @(ignoreReplacedKeyWhenGettingKeyValues), OBJC_ASSOCIATION_ASSIGN );
23
+ }
24
+
25
+ - (BOOL )isIgnoreReplacedKeyWhenGettingKeyValues
26
+ {
27
+ return [objc_getAssociatedObject (self , &MJIgnoreReplacedKeyWhenGettingKeyValuesKey) boolValue ];
28
+ }
29
+
18
30
#pragma mark - --常用的对象--
19
31
static NSNumberFormatter *_numberFormatter;
20
32
+ (void )load
@@ -99,7 +111,7 @@ - (instancetype)setKeyValues:(id)keyValues context:(NSManagedObjectContext *)con
99
111
keyValues = [NSJSONSerialization JSONObjectWithData: keyValues options: kNilOptions error: nil ];
100
112
}
101
113
102
- MJAssertError ([keyValues isKindOfClass: [NSDictionary class ]], self, error, @" keyValues参数不是一个字典 " );
114
+ MJAssertError ([keyValues isKindOfClass: [NSDictionary class ]] || [keyValues isKindOfClass: [ NSArray class ]] , self, error, @" keyValues参数不是一个字典或者数组 " );
103
115
104
116
@try {
105
117
Class aClass = [self class ];
@@ -114,10 +126,9 @@ - (instancetype)setKeyValues:(id)keyValues context:(NSManagedObjectContext *)con
114
126
115
127
// 1.取出属性值
116
128
id value = keyValues ;
117
- NSArray *keys = [property keysFromClass: [self class ]];
118
- for (NSString *key in keys) {
119
- if (![value isKindOfClass: [NSDictionary class ]]) continue ;
120
- value = value[key];
129
+ NSArray *propertyKeys = [property propertyKeysFromClass: [self class ]];
130
+ for (MJPropertyKey *propertyKey in propertyKeys) {
131
+ value = [propertyKey valueForObject: value];
121
132
}
122
133
123
134
// 值的过滤
@@ -259,42 +270,46 @@ + (NSMutableArray *)objectArrayWithFile:(NSString *)file error:(NSError *__autor
259
270
}
260
271
261
272
#pragma mark - 模型 -> 字典
262
- - (NSMutableDictionary * )keyValues
273
+ - (id )keyValues
263
274
{
264
275
return [self keyValuesWithError: nil ];
265
276
}
266
277
267
- - (NSMutableDictionary * )keyValuesWithError : (NSError *__autoreleasing *)error
278
+ - (id )keyValuesWithError : (NSError *__autoreleasing *)error
268
279
{
269
280
return [self keyValuesWithIgnoredKeys: nil error: error];
270
281
}
271
282
272
- - (NSMutableDictionary * )keyValuesWithKeys : (NSArray *)keys
283
+ - (id )keyValuesWithKeys : (NSArray *)keys
273
284
{
274
285
return [self keyValuesWithKeys: keys error: nil ];
275
286
}
276
287
277
- - (NSMutableDictionary * )keyValuesWithIgnoredKeys : (NSArray *)ignoredKeys
288
+ - (id )keyValuesWithIgnoredKeys : (NSArray *)ignoredKeys
278
289
{
279
290
return [self keyValuesWithIgnoredKeys: ignoredKeys error: nil ];
280
291
}
281
292
282
- - (NSMutableDictionary * )keyValuesWithKeys : (NSArray *)keys error : (NSError *__autoreleasing *)error
293
+ - (id )keyValuesWithKeys : (NSArray *)keys error : (NSError *__autoreleasing *)error
283
294
{
284
295
return [self keyValuesWithKeys: keys ignoredKeys: nil error: error];
285
296
}
286
297
287
- - (NSMutableDictionary * )keyValuesWithIgnoredKeys : (NSArray *)ignoredKeys error : (NSError *__autoreleasing *)error
298
+ - (id )keyValuesWithIgnoredKeys : (NSArray *)ignoredKeys error : (NSError *__autoreleasing *)error
288
299
{
289
300
return [self keyValuesWithKeys: nil ignoredKeys: ignoredKeys error: error];
290
301
}
291
302
292
- - (NSMutableDictionary * )keyValuesWithKeys : (NSArray *)keys ignoredKeys : (NSArray *)ignoredKeys error : (NSError *__autoreleasing *)error
303
+ - (id )keyValuesWithKeys : (NSArray *)keys ignoredKeys : (NSArray *)ignoredKeys error : (NSError *__autoreleasing *)error
293
304
{
294
305
// 如果自己不是模型类
295
306
if ([MJFoundation isClassFromFoundation: [self class ]]) return (NSMutableDictionary *)self;
296
307
297
- __block NSMutableDictionary *keyValues = [NSMutableDictionary dictionary ];
308
+ __block id keyValues = nil ;
309
+
310
+ if (self.isIgnoreReplacedKeyWhenGettingKeyValues ) {
311
+ keyValues = [NSMutableDictionary dictionary ];
312
+ }
298
313
299
314
@try {
300
315
Class aClass = [self class ];
@@ -325,26 +340,78 @@ - (NSMutableDictionary *)keyValuesWithKeys:(NSArray *)keys ignoredKeys:(NSArray
325
340
}
326
341
327
342
// 4.赋值
328
- NSArray *keys = [property keysFromClass: [self class ]];
329
- NSUInteger keyCount = keys.count ;
330
- // 创建字典
331
- __block NSMutableDictionary *innerDict = keyValues;
332
- [keys enumerateObjectsUsingBlock: ^(NSString *key, NSUInteger idx, BOOL *stop) {
333
- if (idx == keyCount - 1 ) { // 最后一个属性
334
- innerDict[key] = value;
335
- } else { // 字典
336
- NSMutableDictionary *tempDict = innerDict[key];
337
- if (tempDict == nil ) {
338
- tempDict = [NSMutableDictionary dictionary ];
339
- innerDict[key] = tempDict;
343
+ if (self.isIgnoreReplacedKeyWhenGettingKeyValues ) {
344
+ keyValues[property.name] = value;
345
+ } else {
346
+ NSArray *propertyKeys = [property propertyKeysFromClass: [self class ]];
347
+ NSUInteger keyCount = propertyKeys.count ;
348
+ // 创建字典
349
+ __block id innerContainer = nil ;
350
+ [propertyKeys enumerateObjectsUsingBlock: ^(MJPropertyKey *propertyKey, NSUInteger idx, BOOL *stop) {
351
+ // 创建keyValues
352
+ if (!keyValues) {
353
+ if (propertyKey.type == MJPropertyKeyTypeDictionary) {
354
+ keyValues = [NSMutableDictionary dictionary ];
355
+ } else {
356
+ keyValues = [NSMutableArray array ];
357
+ }
340
358
}
341
- innerDict = tempDict;
342
- }
343
- }];
359
+ // 创建innerContainer
360
+ if (!innerContainer) {
361
+ innerContainer = keyValues;
362
+ if ([innerContainer isKindOfClass: [NSMutableArray class ]]) {
363
+ int index = propertyKey.name .intValue ;
364
+ while ([innerContainer count ] < index + 1 ) {
365
+ [innerContainer addObject: [NSNull null ]];
366
+ }
367
+ }
368
+ }
369
+
370
+ // 下一个属性
371
+ MJPropertyKey *nextPropertyKey = nil ;
372
+ if (idx != keyCount - 1 ) {
373
+ nextPropertyKey = propertyKeys[idx + 1 ];
374
+ }
375
+
376
+ if (nextPropertyKey) { // 不是最后一个key
377
+ // 当前propertyKey对应的字典或者数组
378
+ id tempInnerContainer = [propertyKey valueForObject: innerContainer];
379
+ if (tempInnerContainer == nil || [tempInnerContainer isKindOfClass: [NSNull class ]]) {
380
+ if (nextPropertyKey.type == MJPropertyKeyTypeDictionary) {
381
+ tempInnerContainer = [NSMutableDictionary dictionary ];
382
+ } else {
383
+ tempInnerContainer = [NSMutableArray array ];
384
+ }
385
+ if (propertyKey.type == MJPropertyKeyTypeDictionary) {
386
+ innerContainer[propertyKey.name] = tempInnerContainer;
387
+ } else {
388
+ innerContainer[propertyKey.name.intValue] = tempInnerContainer;
389
+ }
390
+ }
391
+
392
+ if ([tempInnerContainer isKindOfClass: [NSMutableArray class ]]) {
393
+ int index = nextPropertyKey.name .intValue ;
394
+ while ([tempInnerContainer count ] < index + 1 ) {
395
+ [tempInnerContainer addObject: [NSNull null ]];
396
+ }
397
+ }
398
+
399
+ innerContainer = tempInnerContainer;
400
+ } else { // 最后一个key
401
+ if (propertyKey.type == MJPropertyKeyTypeDictionary) {
402
+ innerContainer[propertyKey.name] = value;
403
+ } else {
404
+ innerContainer[propertyKey.name.intValue] = value;
405
+ }
406
+ }
407
+ }];
408
+ }
344
409
}];
345
410
346
411
// 去除系统自动增加的元素
347
- [keyValues removeObjectsForKeys: @[@" superclass" , @" debugDescription" , @" description" , @" hash" ]];
412
+ if ([keyValues isKindOfClass: [NSMutableDictionary class ]]) {
413
+ [keyValues removeObjectsForKeys: @[@" superclass" , @" debugDescription" , @" description" , @" hash" ]];
414
+ }
348
415
349
416
// 转换完毕
350
417
if ([self respondsToSelector: @selector (objectDidFinishConvertingToKeyValues )]) {
0 commit comments