Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/sql-server-2008/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何编写nspredicate以获取包含部分搜索词的记录?_Ios_Objective C_Nspredicate - Fatal编程技术网

Ios 如何编写nspredicate以获取包含部分搜索词的记录?

Ios 如何编写nspredicate以获取包含部分搜索词的记录?,ios,objective-c,nspredicate,Ios,Objective C,Nspredicate,如何编写谓词以使用这样的搜索字符串过滤数据 用户进入“红辣椒” 这应返回所有记录,包括“红辣椒”、“红甜椒”等 这是我写的 NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(ingredientName contains[c] %@)" ,self.searchIngrediants.text ]; [fetchRequest setPredicate:predicate]; 这只返回包含搜索词的记录,如“

如何编写谓词以使用这样的搜索字符串过滤数据

用户进入“红辣椒”

这应返回所有记录,包括“红辣椒”、“红甜椒”等

这是我写的

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(ingredientName contains[c] %@)" ,self.searchIngrediants.text ];
        [fetchRequest setPredicate:predicate];
这只返回包含搜索词的记录,如“red pepper”、“belgian red pepper”

是否可以使用谓词执行此操作。我在用Objective-c


任何帮助都将不胜感激

为此,您需要像一样使用

NSString *searchStringWithPattern = [self.searchIngrediants.text stringByReplacingOccurrencesOfString:@" " withString:@"*"];
searchStringWithPattern = [NSString stringWithFormat:@"*%@*", searchStringWithPattern];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(ingredientName LIKE[cd] %@)" , searchStringWithPattern];
[fetchRequest setPredicate:predicate];

对于正则表达式,它应该类似于:

NSString*    regex     = @"red.*pepper";
NSPredicate* predicate = [NSPredicate predicateWithFormat:@"(ingredientName MATCHES %@)", regex];
如果您的输入有换行符/制表符/。。。相应地调整正则表达式。

目标C代码:-

NSPredicate *predicate;
predicate = [NSPredicate predicateWithFormat:@"ingredientName contains[cd] %@",self.searchIngrediants.text];
Contains将检查字符是否存在

Swift代码:-

var predicate: NSPredicate?
predicate = NSPredicate(format: "ingredientName contains[cd] %@", searchIngrediants.text)

但是“带斑点和条纹的红辣椒”呢?步骤1:当您需要匹配时,请使用非常精确的术语(例如正则表达式)进行定义。是的,您是正确的。它也应该返回这些记录。你能给我举个例子说明如何做到这一点吗?谢谢你给我指出了正确的方向。但是,这只返回名为red pepper的记录。也仅在键入整个搜索词时返回数据。输入“red pep”没有任何作用,除非一直输入“red pepper”。有什么想法吗?@sachinathanaaluvihare检查我添加的编辑答案
searchStringWithPattern=[NSString stringWithFormat:@“*%@*”,searchStringWithPattern]为此,非常感谢!确实为我节省了很多时间。。祝你今天愉快。@sachintanaaluvihare欢迎伙伴:)