NSPredicate for exact match

后端 未结 3 1877
灰色年华
灰色年华 2021-01-24 15:39
 NSArray *arrData = [NSArray arrayWithObjects:
                    @\"cloud,country,plant\",
                    @\"country,cloud,plant\",
                    @\"country         


        
相关标签:
3条回答
  • 2021-01-24 16:00

    Try:

     NSPredicate* predicate = [NSPredicate predicateWithBlock:^(NSString* string, NSDictionary* options){
    
        NSArray* array = [string componentsSeparatedByString:@","];
    
        return [array containsObject:@"cloud"];
    
    }];
    
    0 讨论(0)
  • 2021-01-24 16:06

    Try below code,

    It will work,

     NSPredicate *hsPredicate = [NSPredicate predicateWithBlock:^BOOL(id  _Nonnull evaluatedObject, NSDictionary<NSString *,id> * _Nullable bindings) {
            NSArray *sepretArray =[((NSString*)evaluatedObject) componentsSeparatedByString:@","];
            NSPredicate *subPredicate = [NSPredicate predicateWithFormat:@"self == %@",@"cloud"];
            return  ([sepretArray filteredArrayUsingPredicate:subPredicate].count > 0);
    
        }];
        NSArray *arrResult = [arrData filteredArrayUsingPredicate:hsPredicate];
    
    0 讨论(0)
  • 2021-01-24 16:19

    This one do the trick.But i don't know whether it is the efficient way of doing this.

     NSArray *arrData = [NSArray arrayWithObjects:
                        @"cloud,country,plant",
                        @"country,cloud,plant",
                        @"country,plant,cloud",
                        @"clouds,country,plant"
                        ,@"country,clouds,plant",
                        nil];
    
    NSMutableArray *resArray = [[NSMutableArray alloc]init];
    for(NSString *tempString in arrData)
    {
        NSArray *cache = [tempString componentsSeparatedByString:@","];
        if([cache containsObject:@"cloud"])
          {
            [resArray addObject:tempString];
          }
    }
    
    0 讨论(0)
提交回复
热议问题