0

我有一个 NSDictionary 数组。NSDictionary 有一个名为 multiple_image 的键,其中包含用 , 分隔的字符串。

我想要一组包含 123.png 作为 multiple_images 键的数组。有人可以告诉我如何使用 NSPredicate 或不使用谓词来做到这一点。

//Array 
{
    Image = "<UIImage: 0xf72df30>";
    active = yes;
    "admin_id" = 169;
    "category_id" = 32;
    "chef_id" = 175;
    descr = "Cool tea to cool down the mind.";
    id = 110;
    "multiple_images" = "Jellyfish.jpg,345.png";
    name = "Southern Sweet Ice Tea";
    price = 160;
    rating = 3;
    selected = 0;
    "subcat_id" = 23;
    "tag_id" = 45;
    "tax_id" = 10;
    "tax_value" = "12.00";
},
    {
    Image = "<UIImage: 0xf72ebd0>";
    active = yes;
    "admin_id" = 169;
    "category_id" = 31;
    "chef_id" = 175;
    descr = "Ingredients are almonds or cashews. No hydrogenated stuff, no extra weirdo ingredients";
    id = 107;
    "multiple_images" = "Jellyfish.jpg,123.png";
    name = "Butter Chicken";
    price = 300;
    rating = 3;
    selected = 0;
    "subcat_id" = 24;
    "tag_id" = 43;
    "tax_id" = 9;
    "tax_value" = "0.00";
},
    {
    Image = "<UIImage: 0xf72f870>";
    active = yes;
    "admin_id" = 169;
    "category_id" = 31;
    "chef_id" = 173;
    descr = "Raw vegetables  including carrots, cucumbers.";
    id = 100;
    "multiple_images" = "Jellyfish.jpg,shake.png,";
    name = Salads;
    price = 50;
    rating = 3;
    selected = 0;
    "subcat_id" = 22;
    "tag_id" = 44;
    "tax_id" = 9;
    "tax_value" = "0.00";
}
4

4 回答 4

4

使用谓词,

[array filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"multiple_images CONTAINS '123'"]];

使用谓词,但使用块

    NSArray *filtered = [test filteredArrayUsingPredicate:[NSPredicate predicateWithBlock:
    ^BOOL(NSDictionary *evaluatedObject, NSDictionary * bindings) {
        NSString *key = @"123";
        return ([evaluatedObject[@"multiple_images"] rangeOfString:key].location != NSNotFound);
    }]];
于 2013-05-10T05:40:03.423 回答
1

尝试

NSDictionary *item = mainArray[0];

NSString *imagesString = item[@"multiple_images"];

NSArray *images = [imagesString componentsSeparatedByString:@","];

现在您可以使用图像

于 2013-05-10T05:32:51.940 回答
0

试试这个代码

     for (int i=0; i<[mainArray count]; i++) {
            NSDictionary *item = [mainArray objectAtIndex:i];
            NSString *imagesString = item[@"multiple_images"];
            NSArray *images = [imagesString componentsSeparatedByString:@","];

            for (int j=0;j<[images count]; j++) {
                if ([[images objectAtIndex:j] isEqualToString:@"123.png"]) {
                    ///Your Required code;
                }
            }
      }
于 2013-05-10T05:44:14.437 回答
0

因此,您要保存具有 123.png 的字典。

也许你可以尝试这样的事情:

NSMutableArray *arr = [NSMutableArray new];// your array of objects(dictionaries)
__block NSMutableArray *images123 = [NSMutableArray new];
[arr enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {

    NSMutableDictionary *dictionary = (NSMutableDictionary *)obj;

    if([[dictionary allKeys]containsObject:@"multiple_images"]){
        NSString *imageList = (NSString *)[dictionary objectForKey:@"multiple_images"];
        NSArray *arrayImages = [imageList componentsSeparatedByString:@","];

        if([arrayImages containsObject:@"123.png"]){
            [images123 addObject:dictionary];
        }
    }
}];
于 2013-05-10T05:44:26.117 回答