2021-11-21 08:16:16 +00:00
|
|
|
#import "NSDictionary+Merge.h"
|
|
|
|
|
|
|
|
@implementation NSDictionary (Merge)
|
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
+ (NSDictionary *)dictionaryByMerging:(NSDictionary *)dict1 with:(NSDictionary *)dict2 {
|
|
|
|
NSMutableDictionary *result = [dict1 mutableCopy];
|
2021-11-21 08:16:16 +00:00
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
[dict2 enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
|
|
|
|
if(![dict1 objectForKey:key]) {
|
|
|
|
[result setObject:obj forKey:key];
|
|
|
|
} else if([obj isKindOfClass:[NSDictionary class]]) {
|
|
|
|
NSDictionary *newVal = [[dict1 objectForKey:key] dictionaryByMergingWith:(NSDictionary *)obj];
|
|
|
|
[result setObject:newVal forKey:key];
|
|
|
|
}
|
|
|
|
}];
|
2021-11-21 08:16:16 +00:00
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
return (NSDictionary *)[NSDictionary dictionaryWithDictionary:result];
|
2021-11-21 08:16:16 +00:00
|
|
|
}
|
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
- (NSDictionary *)dictionaryByMergingWith:(NSDictionary *)dict {
|
|
|
|
return [[self class] dictionaryByMerging:self with:dict];
|
2021-11-21 08:16:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@end
|