NSDictionary:查找特定对象并将其删除 [英] NSDictionary: Find a specific object and remove it

查看:80
本文介绍了NSDictionary:查找特定对象并将其删除的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 NSMutableDictionary 看起来像这样:

I have NSMutableDictionary that looks like this:

category =     (
            {
        description =             (
                            {
                id = 1;
                name = Apple;
            },
                            {
                id = 5;
                name = Pear;
            },
                            {
                id = 12;
                name = Orange;
            }
        );
        id = 2;
        name = Fruits;
    },
            {
        description =             (
                            {
                id = 4;
                name = Milk;
            },
                            {
                id = 7;
                name = Tea;
            }
        );
        id = 5;
        name = Drinks;
    }
);

现在,当用户在我的应用程序中执行操作时,我会得到对象的 @"name" 值,例如Apple".我想从字典中删除该对象,但是如何使用

Now, when a user performs an action in my application, I get the @"name"-value of the object, like "Apple". I would like to remove that object from the dictionary, but how will can I reach this object with the

[myDictionary removeObjectForKey:]

方法?

推荐答案

在高层次上,您需要获得对描述"数组的引用.然后遍历数组获取每个字典.检查字典以查看它是否具有匹配的名称"值.如果是这样,请从描述"数组中删除该字典.

At a high level you need to get a reference to the "description" array. Then iterate through the array getting each dictionary. Check the dictionary to see if it has the matching "name" value. If so, remove that dictionary from the "description" array.

NSString *name = ... // the name to find and remove
NSMutableArray *description = ... // the description array to search
for (NSUInteger i = 0; i < description.count; i++) {
    NSDictionary *data = description[i];
    if ([data[@"name"] isEqualToString:name]) {
        [description removeObjectAtIndex:i];
        break;
    }
}

这篇关于NSDictionary:查找特定对象并将其删除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆