zoukankan      html  css  js  c++  java
  • iOS :Object-C 语言merge两个字典对象

    Object-C 语言merge两个字典对象

    
    - (id)mutableDictionaryCopyIfNeeded:(id)dictObj
    {
        if ([dictObj isKindOfClass:[NSDictionary class]] &&
            ![dictObj isKindOfClass:[NSMutableDictionary class]]) {
            dictObj = [dictObj mutableCopy];
        }
        return dictObj;
    }
    
    /**
     合并两个字典
    
     @param dict 被合并的字典
     */
    - (void)mergingWithDictionary:(NSDictionary *)dict
    {
        for (id key in [dict allKeys]) {
            id obj = [self mutableDictionaryCopyIfNeeded:[dict objectForKey:key]];
            id localObj = [self mutableDictionaryCopyIfNeeded:[self objectForKey:key]];
            if ([obj isKindOfClass:[NSDictionary class]] &&
                [localObj isKindOfClass:[NSMutableDictionary class]]) {
                // Recursive merge for NSDictionary
                [localObj mergingWithDictionary:obj];
            } else if (obj) {
                [self setObject:obj forKey:key];
            }
        }
    }
    
    /**
     合并两个字典
    
     @param dict       被合并的字典
     @param ignoredKey 忽略的Key
     */
    - (void)mergingWithDictionary:(NSDictionary *)dict ignoredDictKey:(NSString *)ignoredKey
    {
        for (id key in [dict allKeys]) {
            if ([key isEqualToString:ignoredKey]) {
                continue;
            }
            id obj = [self mutableDictionaryCopyIfNeeded:[dict objectForKey:key]];
            id localObj = [self mutableDictionaryCopyIfNeeded:[self objectForKey:key]];
            if ([obj isKindOfClass:[NSDictionary class]] &&
                [localObj isKindOfClass:[NSMutableDictionary class]]) {
                // Recursive merge for NSDictionary
                [localObj mergingWithDictionary:obj];
            } else if (obj) {
                [self setObject:obj forKey:key];
            }
        }
    }
    
  • 相关阅读:
    GPUImage源码解读之GPUImageFramebuffer
    CSS之定位
    CSS之浮动
    CSS之盒子模型
    CSS之元素
    CSS之选择器
    iOS图片瘦身总结
    iOS动画——CoreAnimation
    iOS动画——DynamicAnimate
    iOS动画——UIKit动画
  • 原文地址:https://www.cnblogs.com/ShaoYinling/p/5936421.html
Copyright © 2011-2022 走看看