IOS / Core Data: how to create a predicate for words starting with a number of characters?

Like is pretty simple, does it seem to support only? and * I think it's kind of like old-school wildcards.

But I want to do this: find all words starting with a given range of characters ... for example, aj.

So, I found it to find all words starting with the letter j:

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"Name LIKE 'j*'"];

However, I can’t figure out how to combine all words starting with a certain range, for example aj: "Name LIKE" [aj] * '"does not work.

any ideas how i could achieve this? Or my only way to do something like: "Name LIKE 'a *' OR name LIKE 'b *' OR name LIKE 'c *' OR name LIKE 'd *' ..." because that it would be unsuccessful ...

Thank!!!

+3
source share
3 answers

MATCHES will allow you to use regular expressions.

+3
source

An example for simple copypasta:

//title starts with number
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"title MATCHES '^[0-9].*'"];

//title starts with letter a-j or A-J
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"title MATCHES '^[a-jA-J].*'"];
+6
source

MATCHES only works for arrays of Core Data objects !!!

For example, you cannot use it to query the sqlite database to get these arrays or you will get the following exception:

Unimplemented SQL generation for predicate (SELF MATCHES ...
+1
source

Source: https://habr.com/ru/post/1767319/


All Articles