大家好,这是我的问题
NSArray *recipeIngredientsArray = [recipeIngString componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@","]];
NSArray *haveIngArray = [searchText componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@","]];
if (haveIngArray.count==recipeIngredientsArray.count)
{
//check the contents of 2 arrays
}
我从CoreData中获得了两个数组,如果它们的array length
相同,我将检查内容。
在haveIngArray
中,我会有像"米饭"、"鸡肉"、"生菜"这样的字符串
在recipeIngredientsArray
中,我有"1勺米饭"、"150克鸡肉"、"1杯牛奶"等字符串
recipeIngredientsArray
中是否有类似"米饭"one_answers"鸡肉"的字符串?
我尝试将NSPredicate
与contain一起使用,但结果不太好。
我对的建议持开放态度
感谢
recipeIngredientsArray
不应包含字符串,而应包含其中一个Key/值为@"ingredientName":@"rice"
的字典或具有属性name
的Ingredient类的更好对象。比谓词查询要容易得多。
#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,
)