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];
            }
        }
    }
    
  • 相关阅读:
    Scheduled定时任务的触发规则
    linux设置系统时间为当前网络时间
    idea控制台打印日志出现乱码
    Nginx配置高可用的集群
    Nginx实现负载均衡
    BigDecimal比较大小
    Java调用第三方系统接口获取数据
    sql中主要关键字的执行顺序
    ASP.NET.Core --Jenkins+Docker
    ASP.NET.Core --Swagger+Route
  • 原文地址:https://www.cnblogs.com/ShaoYinling/p/5936421.html
Copyright © 2011-2022 走看看