Inverse string comparison type using NSPredicate

I searched this answer all over the internet, but so far no luck. Therefore, I need to consult smart and nice people here. This is my first question asking the question here, so I hope I do it right and not repeat the question.

For all the examples I've seen, this is the search bar, which is a substring of what is stored in Core Data. On the other hand, I want to achieve the following:

The rows stored in the master data are actually substrings. I want to do a search by getting all the main data strings that have substrings belonging to the provided search string.

For example: In the master data, I have "AB", "BC", "ABC", "ABCDEF", "GH", "ABA", And in the application I search by providing the superstring: "ABCDEF", the result will return " AB "," BC "," ABC "," ABCDEF ", but not" GH "," ABA ", because these two substrings are not superstrings.

How do I configure the predicateWithFormat statement?

This does not work because it does the opposite:

NSPredicate *myPredicate = [NSPredicate predicateWithFormat:@"substring LIKE[c] %@", @"ABCDEF"];

Thanks everyone!

+5
source share
2 answers

Reverse CONTAINSwill not work. In addition, you will not be able to use LIKE, because you will need to take the attribute you are looking for and convert it to a wildcard.

- MATCHES, . , * . .

.

NSString *string= @"ABCDEF";
NSMutableString *new = [NSMutableString string];
for (int i=0; i<string.length; i++) {
    [new appendFormat:@"%c*", [string characterAtIndex:i]]; 
}
// new is now @"A*B*C*D*E*F*";
fetchRequest.predicate = [NSPredicate predicateWithFormat:
                          @"stringAttribute matches %@", new];

stringAttribute - NSString .

+4

, :

NSPredicate *pred = [NSPredicate predicateWithFormat:@"%@ contains self",@"ABCDEF"];

:

-(IBAction)doFetch:(id)sender {
    NSFetchRequest *request = [[NSFetchRequest alloc] init];
    request.entity = [NSEntityDescription entityForName:@"Expense" inManagedObjectContext:self.managedObjectContext];
    request.predicate = [NSPredicate predicateWithFormat:@"%@ contains desc",@"ABCDEF"];
    NSArray *answer = [self.managedObjectContext executeFetchRequest:request error:nil];
    NSLog(@"%@",answer);
}

"desc" "". , "desc" - "ABCDEF".

0

All Articles