How to create an NSPredicate to search for records with a leading numeric value?

I use NSPredicates to retrieve entities based on the name attribute. Creating a predicate for names starting with letters was easy (@ "name BEGINSWITH% @", searchLetter), but now I would like to get all objects with a name that starts with a numeric value, or rather a non-alphabetic character.

What would be the appropriate predicate expression here?

Now I don’t want to go too deep into predicate programming, as that’s all I need now, and time flies. So please don't point me to the Predicate Programming Guide, I just need this expression .. :)

Thanks a lot of guys!

+3
source share
2 answers

If the operator MATCHESdoes not work with the selection request, you will essentially have to manually manually:

NOT(name BEGINSWITH[cd] 'a' OR name BEGINSWITH[cd] 'b' OR ... OR name BEGINSWITH[cd] 'z')

It will get everything that doesn't start with a letter. Similar:

name BEGINSWITH[cd] '0' OR name BEGINSWITH[cd] '1' OR ... OR name BEGINSWITH[cd] '9'

Would get everything that starts with a number.

change the comment triggered another idea:

If you could extract the first letter name, you could do:

NOT(name[0] IN %@), arrayOfLetters

( name[0]is the first character name, and arrayOfLettersis an NSArrayalphabetic character)

+5
source

You can use 'MATCHES' where you can use any regular expression (eg ^ [0-9]) to compare. But as far as I know, this does not work when working with Core Data.

(OS X 10.6, iOS 4.0) predicateWithBlock: .

+1

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


All Articles