如何比较2个NSArrays,如NSPredicate Contains

时间:2014-04-26 16:07:25

标签: ios objective-c cocoa-touch

大家好,这是我的问题

NSArray *recipeIngredientsArray = [recipeIngString componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@","]];
NSArray  *haveIngArray = [searchText componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@","]];

if (haveIngArray.count==recipeIngredientsArray.count) 
{       
//check the contents of 2 arrays 

}

我从CoreData获取了2个数组,如果他们的array length相同,我会检查内容。

haveIngArray中,我会有“米饭”,“鸡肉”,“生菜”之类的字符串 在recipeIngredientsArray我有“1勺米饭”,“150克鸡肉”,“1杯牛奶”之类的字符串

有没有看到recipeIngredientsArray中有“米饭”和“鸡肉”这样的字符串?

我尝试将NSPredicate与包含一起使用,但结果并不好。

我愿意接受建议

谢谢

1 个答案:

答案 0 :(得分:1)

recipeIngredientsArray不应包含字符串,而是包含一个键/值为@"ingredientName":@"rice"的字典,或者包含属性name的更好的类成分对象。比起谓词更容易被查询。

#import <Foundation/Foundation.h>

@interface RecipeIngredient : NSObject
@property (nonatomic, copy) NSString *name;
@property (nonatomic, strong) NSNumber *amount;
@property (nonatomic, copy) NSString *unit;
@end


@implementation RecipeIngredient

-(NSString *)description
{
    return [NSString stringWithFormat:@"%@ %@ %@", _amount, _unit, _name];
}
@end



int main(int argc, const char * argv[])
{

    @autoreleasepool {
        NSArray *recipeIngredientsArray = @[
                                            ({
                                                RecipeIngredient *ing = [[RecipeIngredient alloc] init];
                                                ing.amount = @(1);
                                                ing.name = @"rice";
                                                ing.unit = @"spoon";
                                                ing;
                                            }),
                                            ({
                                                RecipeIngredient *ing = [[RecipeIngredient alloc] init];
                                                ing.amount = @(150);
                                                ing.name = @"chicken";
                                                ing.unit = @"gr";
                                                ing;
                                            }),
                                            ({
                                                RecipeIngredient *ing = [[RecipeIngredient alloc] init];
                                                ing.amount = @(1);
                                                ing.name = @"milk";
                                                ing.unit = @"cup";
                                                ing;
                                            }),
                                            ];

        NSArray *haveIngArray = @[@"rice", @"chicken", @"lettuce"];

        NSPredicate *predicate = [NSPredicate predicateWithFormat:@" self.name  in %@", haveIngArray];

        NSArray *filteredArray = [recipeIngredientsArray filteredArrayUsingPredicate:predicate];

        for (RecipeIngredient *ing in filteredArray) {
            NSLog(@"%@", ing);
        }
    }
    return 0;
}

输出:

1 spoon rice
150 gr chicken

示例请知道配方中没有的东西或冰箱中没有的东西:

NSArray *recipeIngredientsArray = @[
                                    ({
                                        RecipeIngredient *ing = [[RecipeIngredient alloc] init];
                                        ing.amount = @(1);
                                        ing.name = @"rice";
                                        ing.unit = @"spoon";
                                        ing;
                                    }),
                                    ({
                                        RecipeIngredient *ing = [[RecipeIngredient alloc] init];
                                        ing.amount = @(150);
                                        ing.name = @"chicken";
                                        ing.unit = @"gr";
                                        ing;
                                    }),
                                    ({
                                        RecipeIngredient *ing = [[RecipeIngredient alloc] init];
                                        ing.amount = @(1);
                                        ing.name = @"milk";
                                        ing.unit = @"cup";
                                        ing;
                                    }),
                                    ];

NSArray *haveIngArray = @[@"rice", @"chicken", @"lettuce"];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"not (self.name  in %@)", haveIngArray];
NSArray *filteredArray = [recipeIngredientsArray filteredArrayUsingPredicate:predicate];
NSArray *filteredIngNames = [filteredArray valueForKey:@"name"];
NSLog(@"in recipe but not in fridge: %@", filteredIngNames);

predicate = [NSPredicate predicateWithFormat:@"not (self  in %@.name)", recipeIngredientsArray];
filteredArray = [haveIngArray filteredArrayUsingPredicate:predicate];
NSLog(@"in fridge but not in recipe: %@", filteredArray);

输出:

in recipe but not in fridge: (
    milk
)
in fridge but not in recipe: (
    lettuce
)

如何从您拥有的字符串创建自定义成分对象:

NSNumberFormatter * f = [[NSNumberFormatter alloc] init];
[f setNumberStyle:NSNumberFormatterDecimalStyle];

NSArray *recipe = @[@"1 spoon of rice", @"150 gr chicken", @"1 cup of milk"];
NSMutableArray *recipeIngredientsArray = [@[] mutableCopy];

for (NSString *string in recipe) {
    NSArray *a = [string componentsSeparatedByString:@" "];
    RecipeIngredient *ing = [[RecipeIngredient alloc] init];
    ing.amount = [f numberFromString:a[0]];
    ing.name = [a lastObject];
    ing.unit = [[a subarrayWithRange:NSMakeRange(1, [a count]-2)] componentsJoinedByString:@" "];
    [recipeIngredientsArray addObject:ing];

}

记录recipeIngredientsArray打印

(
    1 spoon of rice,
    150 gr chicken,
    1 cup of milk,
)