NSDictionary+Merge: Support overwriting empty

Support overwriting empty fields of NSNumber or NSString form with
values from the merging dictionary. Correctly overwrite the value from
the first dictionary with values from the second if the first contains
empty strings or zeroed numbers.

Signed-off-by: Christopher Snowhill <kode54@gmail.com>
CQTexperiment
Christopher Snowhill 2022-02-14 19:55:20 -08:00
parent a8c9f748c7
commit 67fcd2bb27
1 changed files with 13 additions and 0 deletions

View File

@ -11,6 +11,19 @@
} else if([obj isKindOfClass:[NSDictionary class]]) { } else if([obj isKindOfClass:[NSDictionary class]]) {
NSDictionary *newVal = [[dict1 objectForKey:key] dictionaryByMergingWith:(NSDictionary *)obj]; NSDictionary *newVal = [[dict1 objectForKey:key] dictionaryByMergingWith:(NSDictionary *)obj];
[result setObject:newVal forKey:key]; [result setObject:newVal forKey:key];
} else {
BOOL isEmpty = NO;
id objTarget = [dict1 objectForKey:key];
if([objTarget isKindOfClass:[NSString class]]) {
NSString *val = (NSString *)objTarget;
isEmpty = [val length] == 0;
} else if([objTarget isKindOfClass:[NSNumber class]]) {
NSNumber *val = (NSNumber *)objTarget;
isEmpty = [val isEqualTo:@(0)];
}
if(isEmpty) {
[result setObject:obj forKey:key];
}
} }
}]; }];