更改 NSDictionary 中的键名称

发布于 2024-11-01 01:43:39 字数 83 浏览 3 评论 0原文

我有一个方法,它返回一个带有某些键和值的 nsdictionary。我需要将字典中的键名称更改为新的键名称,但该键的值需要相同,但我被困在这里。需要帮助

I have a method which returns me a nsdictionary with certain keys and values. i need to change the key names from the dictionary to a new key name but the values need to be same for that key,but i am stuck here.need help

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

夜唯美灬不弃 2024-11-08 01:43:39

此方法仅适用于可变字典。如果新密钥已经存在,它不会检查应该做什么。

您可以通过调用 mutableCopy 来获取不可变对象的可变字典。

- (void)exchangeKey:(NSString *)aKey withKey:(NSString *)aNewKey inMutableDictionary:(NSMutableDictionary *)aDict
{
    if (![aKey isEqualToString:aNewKey]) {
        id objectToPreserve = [aDict objectForKey:aKey];
        [aDict setObject:objectToPreserve forKey:aNewKey];
        [aDict removeObjectForKey:aKey];
    }
}

This method will only work with a mutable dictionary. It doesn't check what should be done if the new key already exists.

You can get a mutable dictionary of a immutable by calling mutableCopy on it.

- (void)exchangeKey:(NSString *)aKey withKey:(NSString *)aNewKey inMutableDictionary:(NSMutableDictionary *)aDict
{
    if (![aKey isEqualToString:aNewKey]) {
        id objectToPreserve = [aDict objectForKey:aKey];
        [aDict setObject:objectToPreserve forKey:aNewKey];
        [aDict removeObjectForKey:aKey];
    }
}
梦里°也失望 2024-11-08 01:43:39

为了将特定键更改为新键,我为类别类编写了一个递归方法。


- (NSMutableDictionary*)replaceKeyName:(NSString *)old_key with:(NSString )new_key {
    NSMutableDictionary dict = [NSMutableDictionary dictionaryWithDictionary: self];
    NSMutableArray *keys = [[dict allKeys] mutableCopy];
    for (NSString key in keys) {
        if ([key isEqualToString:old_key]) {
            id val = [self objectForKey:key];
            [dict removeObjectForKey:key];
            [dict setValue:val forKey:new_key];
            return dict;
        } else {
            const id object = [dict objectForKey: key];
            if ([object isKindOfClass:[NSDictionary class]]) {
                [dict setObject:[dict replaceKeyName:old_key with:new_key] forKey:key];
            } else if ([object isKindOfClass:[NSArray class]]){
                if (object && [(NSArray)object count] > 0) {
                    NSMutableArray *arr_temp = [[NSMutableArray alloc] init];
                    for (NSDictionary *temp_dict in object) {
                        NSDictionary *temp = [temp_dict replaceKeyName:old_key with:new_key];
                        [arr_temp addObject:temp];
                    }
                    [dict setValue:arr_temp forKey:key];
                }
            }
        }
    }
    return dict;
}

To change specific key to new key, I have written a recursive method for Category Class.


- (NSMutableDictionary*)replaceKeyName:(NSString *)old_key with:(NSString )new_key {
    NSMutableDictionary dict = [NSMutableDictionary dictionaryWithDictionary: self];
    NSMutableArray *keys = [[dict allKeys] mutableCopy];
    for (NSString key in keys) {
        if ([key isEqualToString:old_key]) {
            id val = [self objectForKey:key];
            [dict removeObjectForKey:key];
            [dict setValue:val forKey:new_key];
            return dict;
        } else {
            const id object = [dict objectForKey: key];
            if ([object isKindOfClass:[NSDictionary class]]) {
                [dict setObject:[dict replaceKeyName:old_key with:new_key] forKey:key];
            } else if ([object isKindOfClass:[NSArray class]]){
                if (object && [(NSArray)object count] > 0) {
                    NSMutableArray *arr_temp = [[NSMutableArray alloc] init];
                    for (NSDictionary *temp_dict in object) {
                        NSDictionary *temp = [temp_dict replaceKeyName:old_key with:new_key];
                        [arr_temp addObject:temp];
                    }
                    [dict setValue:arr_temp forKey:key];
                }
            }
        }
    }
    return dict;
}

远昼 2024-11-08 01:43:39

您无法更改 NSDictionary 中的任何内容,因为它是只读的。

循环遍历字典并使用新的键名创建一个新的 NSMutableDictionary 怎么样?

You can't change anything in an NSDictionary, since it is read only.

How about loop through the dictionary and create a new NSMutableDictionary with the new key names ?

ゝ偶尔ゞ 2024-11-08 01:43:39

您不能使用旧值添加新的键值对,然后删除旧的键值对吗?

这只适用于 NSMutableDictionaryNSDictionarys 的设计初衷是一旦创建就不能更改。

Could you not add a new key-value pair using the old value, and then remove the old key-value pair?

This would only work on an NSMutableDictionary. NSDictionarys are not designed to be changed once they have been created.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文