Ios 如何在搜索列表中首先获得同名结果

Ios 如何在搜索列表中首先获得同名结果,ios,objective-c,sorting,nspredicate,Ios,Objective C,Sorting,Nspredicate,大家好,我需要你们的帮助。我希望像在图像中一样,如果我搜索carrot列表,应该首先显示carrot,就像具有相同名称的元素一样,首先显示,如果元素包含这些文本列表,那么这是可能的 NSString *predicateString; NSString * tempString; if (string.length > 0) { tempString = [NSString stringWithForma

大家好,我需要你们的帮助。我希望像在图像中一样,如果我搜索carrot列表,应该首先显示carrot,就像具有相同名称的元素一样,首先显示,如果元素包含这些文本列表,那么这是可能的

 NSString *predicateString;

            NSString * tempString;

            if (string.length > 0) {
                tempString = [NSString stringWithFormat:@"%@%@",textField.text, string];
            } else {
                tempString = [textField.text substringToIndex:[textField.text length] - 1];
            }

                predicateString = [NSString stringWithFormat:@"SELF.title contains [cd] \"%@\" ", tempString];

            NSLog(@"Ingredent Array  :- %@ ",allIngrediantArr);

            NSPredicate *predicate = [NSPredicate predicateWithFormat:predicateString];
            if (allIngrediantArr.count>0) {
                searchFilterdArr = [NSMutableArray arrayWithArray:[allIngrediantArr filteredArrayUsingPredicate:predicate]];
            }

您可以在备用模式中使用

从开始

predicateString = [NSString stringWithFormat:@"SELF.title BEGINSWITH [cd] \"%@\" ", tempString];
  NSPredicate *predicate = [NSPredicate predicateWithFormat:
                      @"(SELF.title BEGINSWITH [cd] \"%@\") OR (SELF.title MATCHES [cd] \"%@\")", tempString, tempString];
包含| |开始于

predicateString = [NSString stringWithFormat:@"SELF.title BEGINSWITH [cd] \"%@\" ", tempString];
  NSPredicate *predicate = [NSPredicate predicateWithFormat:
                      @"(SELF.title BEGINSWITH [cd] \"%@\") OR (SELF.title MATCHES [cd] \"%@\")", tempString, tempString];
如果您想要两者(包含| | Beginswith),则使用

NSPredicate *predicate = [NSPredicate predicateWithFormat:
                      @"(SELF.title BEGINSWITH [cd] \"%@\") OR (SELF.title CONTAINS [cd] \"%@\")", tempString, tempString];
匹配| |从开始

predicateString = [NSString stringWithFormat:@"SELF.title BEGINSWITH [cd] \"%@\" ", tempString];
  NSPredicate *predicate = [NSPredicate predicateWithFormat:
                      @"(SELF.title BEGINSWITH [cd] \"%@\") OR (SELF.title MATCHES [cd] \"%@\")", tempString, tempString];
请参阅
NSPredicate


您可以在备用模式中使用

从开始

predicateString = [NSString stringWithFormat:@"SELF.title BEGINSWITH [cd] \"%@\" ", tempString];
  NSPredicate *predicate = [NSPredicate predicateWithFormat:
                      @"(SELF.title BEGINSWITH [cd] \"%@\") OR (SELF.title MATCHES [cd] \"%@\")", tempString, tempString];
包含| |开始于

predicateString = [NSString stringWithFormat:@"SELF.title BEGINSWITH [cd] \"%@\" ", tempString];
  NSPredicate *predicate = [NSPredicate predicateWithFormat:
                      @"(SELF.title BEGINSWITH [cd] \"%@\") OR (SELF.title MATCHES [cd] \"%@\")", tempString, tempString];
如果您想要两者(包含| | Beginswith),则使用

NSPredicate *predicate = [NSPredicate predicateWithFormat:
                      @"(SELF.title BEGINSWITH [cd] \"%@\") OR (SELF.title CONTAINS [cd] \"%@\")", tempString, tempString];
匹配| |从开始

predicateString = [NSString stringWithFormat:@"SELF.title BEGINSWITH [cd] \"%@\" ", tempString];
  NSPredicate *predicate = [NSPredicate predicateWithFormat:
                      @"(SELF.title BEGINSWITH [cd] \"%@\") OR (SELF.title MATCHES [cd] \"%@\")", tempString, tempString];
请参阅
NSPredicate


您可以通过比较搜索字符串的范围来筛选
searchFilterdArr
数组

  NSString *searchStr = @"carrot";  
  NSArray *searchFilterdArr = [[NSArray alloc ] initWithObjects:@{@"title":@"baby carrot"},@{@"title":@"baby purple carrot"},@{@"title":@"carrot"}, nil];
  NSLog(@"%@",searchFilterdArr);
  id mySort = ^(NSDictionary * obj1, NSDictionary * obj2){
    return [[obj1 valueForKey:@"title"] rangeOfString:searchStr].location > [[obj2 valueForKey:@"title"] rangeOfString:searchStr].location;
  };
  NSArray * sortedMyObjects = [searchFilterdArr sortedArrayUsingComparator:mySort];
  NSLog(@"%@",sortedMyObjects);
搜索过滤器阵列

{ title=“小胡萝卜”; }, { title=“小紫胡萝卜”; }, { title=“胡萝卜”; }

分类对象

{ title=“胡萝卜”; }, { title=“小胡萝卜”; }, { title=“小紫胡萝卜”; }


您可以通过比较搜索字符串的范围来筛选
searchFilterdArr
数组

  NSString *searchStr = @"carrot";  
  NSArray *searchFilterdArr = [[NSArray alloc ] initWithObjects:@{@"title":@"baby carrot"},@{@"title":@"baby purple carrot"},@{@"title":@"carrot"}, nil];
  NSLog(@"%@",searchFilterdArr);
  id mySort = ^(NSDictionary * obj1, NSDictionary * obj2){
    return [[obj1 valueForKey:@"title"] rangeOfString:searchStr].location > [[obj2 valueForKey:@"title"] rangeOfString:searchStr].location;
  };
  NSArray * sortedMyObjects = [searchFilterdArr sortedArrayUsingComparator:mySort];
  NSLog(@"%@",sortedMyObjects);
搜索过滤器阵列

{ title=“小胡萝卜”; }, { title=“小紫胡萝卜”; }, { title=“胡萝卜”; }

分类对象

{ title=“胡萝卜”; }, { title=“小胡萝卜”; }, { title=“小紫胡萝卜”; }



显示您的试用代码…。@Anbu.karthik请立即检查显示您的试用代码…。@Anbu.karthik请立即检查,但我还需要包含特殊单词的其他结果,不仅是开始with@FahimParkar-嗨,我的大四,你好吗u@GurinderBatth-您面临的问题是什么,能否更新code@GurinderBatth-如果您更新了?这很容易理解,但我还需要其他的结果,其中包含特殊的词不仅开始with@FahimParkar-嗨,我的大四,你好吗u@GurinderBatth-您面临的问题是什么,能否更新code@GurinderBatth-如果您更新了?很容易理解,如果NSDictionaries不是数组,那么我有一个数组Strings@GurinderBatth用NSDictionaryYeah更新了答案我正在检查它并让你知道谢谢先生稍作修改后它对meI的作用有一个数组如果NSDictionaries没有数组Strings@GurinderBatth用NSDictionaryYeah更新了答案我正在检查并让你知道了,谢谢,先生,稍作修改后对我有用