如何比较2 NSArrays像NSPredicate包含 [英] How to compare 2 NSArrays like NSPredicate Contains
问题描述
大家好,这是我的问题
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个数组,并且它们的数组长度
相同,我将检查内容。
I have 2 arrays that I take from CoreData and if their array length
are the same, I will check the content.
在 haveIngArray
中,我将使用大米之类的字符串,鸡肉,生菜。
在 recipeIngredientsArray
中,我有类似 1汤匙饭, 150 gr鸡肉, 1的字符串一杯牛奶
In haveIngArray
, I will have strings like "rice", "chicken","lettuce".
In recipeIngredientsArray
I have strings like "1 spoon of rice","150 gr chicken","1 cup of milk"
在任何地方都可以在 recipeIngredientsArray
中看到诸如大米和鸡肉之类的字符串?
Is there anyway to see strings like "rice" and "chicken" is available in recipeIngredientsArray
?
我尝试将 NSPredicate
与contains一起使用,但效果并不理想。
I tried to use NSPredicate
with contain but it didn't turn out well.
我愿意接受建议
谢谢
推荐答案
recipeIngredientsArray
不应包含字符串,而应包含两个字典,其中一个键/值是 @ ingredientName:@ rice
或属性名称
的成分类的更好对象。
recipeIngredientsArray
should not contain strings but either dictionaries where one Key/value is @"ingredientName":@"rice"
or better objects of class Ingredient with a property name
. Than it will become much easier to be queried by predicates.
#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;
}
输出:
output:
1 spoon rice
150 gr chicken
如何知道食谱中没有或冰箱中不存在的东西的例子:
Example ho to know what is not in the recipe or not present in the fridge:
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);
输出:
output:
in recipe but not in fridge: (
milk
)
in fridge but not in recipe: (
lettuce
)
如何创建自定义成分对象构成您拥有的字符串:
How to create custom ingredient objects form the strings you have:
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];
}
记录配方成分数组打印
(
1 spoon of rice,
150 gr chicken,
1 cup of milk,
)
这篇关于如何比较2 NSArrays像NSPredicate包含的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!