如何将文本中仅选定的字符串与objective-c中给定的输入进行比较



我知道这个问题听起来很老套。我找不到更好的表达方式了,所以我会慢慢来解释我正在努力解决的问题。

我有一个接受用户输入的iPhone应用程序。我得到了一个plist(我很快就会把它转换成一个在线数据库)我现在做的就是这个。我将输入字符串与plist中项目的成分部分进行比较。

这是plist格式

<array>
    <dict>
        <key>category</key>
        <string>desert</string>
        <key>numberOfPerson</key>
        <string>3</string>
        <key>recipeImage</key>
        <string>asd.jpg</string>
        <key>time</key>
        <string>15</string>
        <key>recipeName</key>
        <string>Puding</string>
        <key>recipeDetail</key>

我将输入与recipeIngredients进行比较。但我的代码所做的并不是我所需要的。如果比较结果为真,我只列出plist中包含输入成分的每一项。我可以过滤选定的食谱,但我想要的是:除非有与输入和配料完全匹配的内容,否则我不想显示出来。

问题是这样的。我得到了这样的食谱配料:1勺糖,1勺盐,100克鸡肉。

用户输入诸如盐、糖之类的输入。鸡肉,所以我不能完全比较它。它永远不会一样,所以我什么都不能展示。

我怎样才能做到这一点。

我愿意接受任何建议。

这就是我比较的方式

    results = [arrayOfPlist filteredArrayUsingPredicate:[NSPredicate predicateWithBlock:^BOOL(id evaluatedObject, NSDictionary *bindings) {
        NSDictionary *_dataRow = (NSDictionary *)evaluatedObject;
        return ([[[_dataRow valueForKey:@"recipeIngredients"] lowercaseString] rangeOfString:[searchText lowercaseString]].location != NSNotFound);
    }]];

其中searchText是我的输入。

首先,您永远不会知道用户输入中是否存在拼写错误。

但是,在比较两个字符串之前,可以对给定的字符集进行一点微调。NSString类中有一个方法称为:

- (NSString *)stringByTrimmingCharactersInSet:(NSCharacterSet *)set

如果你想摆脱。或-字符,则需要在字符集中指定它们。然后,你可以比较两个字符串。

使用-[NSPredicate predicateWithFormat:]可以进行数据库式的字符串比较。例如,你可以尝试

[NSPredicate predicateWithFormat:@"recipeIngredients CONTAINS[cd] %@", searchText]

结账https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/Predicates/Articles/pSyntax.html名为"字符串比较"的部分

编辑:如果用户一次搜索多个东西,比如"鸡肉、面条",你可以更花哨一点,然后做:

NSArray *tokens = [[searchText componentsSeparatedByCharactersInSet:NSCharacterSet.alphanumericCharacterSet.invertedSet] filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"length > 0"];
NSPredicate *searchPredicate = [NSPredicate predicateWithFormat:@"recipeIngredient CONTAINS[cd] (ANY %@)", tokens]

您应该使用-componentsSeparatedByString:@","searchText拆分为一个数组,然后在数组中循环查看recipeIngredients是否包含searchText数组中的任何成分。为了确定查询是否包含每一个成分,您可以在块内部创建一个整数,并在每次匹配时递增。如果匹配的数量等于配料的数量,那么你可以从那里开始。

下面的代码构建了一个谓词,可以归结为"配料包含糖,配料包含巧克力"

NSArray*        recipes = @[
                            @{@"recipeIngredients": @"sugar flour chocolate"},
                            @{@"recipeIngredients": @"sugar chocolate"},
                            @{@"recipeIngredients": @"flour chocolate"},
                            @{@"recipeIngredients": @"chocolate"},
                            ];
NSString*       search = @"sugar, chocolate";
// split the ingredients we have into an array of strings separated by ',' or ' '
NSArray*        haves = [search componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@", "]];
// Build a list of recipeIngredients CONTAINS have
NSMutableArray* ands = [NSMutableArray new];
for(NSString* have in haves)
{
    if(have.length > 0)
    {
        [ands addObject:[NSPredicate predicateWithFormat:@"recipeIngredients CONTAINS[cd] %@", have]];
    }
}
// String all the haves into a single long ... AND ... AND ... predicate    
NSPredicate*    predicate = [NSCompoundPredicate andPredicateWithSubpredicates:ands];
// Apply the predicate
NSArray*        filtered = [recipes filteredArrayUsingPredicate:predicate];

最新更新