开发者

iOS / Core Data: How to create a predicate for words that start with a range of characters?

The 'Like' is pretty simple it seems, it only supports ? and * I believe, kind of like old-school wildcards.

But I want to do this: Find all words that begin with a given range of characters... for example a-j.

So I got it to find all words that start with say, the letter j:

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

However, I can't figure out how to match all words that begin with a certain range, for example a-j: "Name LIKE '[a-j]*'" doesn't work..

any ideas how I might achieve this? Or will my only way be to do something like: "Name LIKE 'a*' OR Name LIKE 'b*' OR Name LIKE 'c*' OR Name LIKE 'd*' ... " cause开发者_JAVA百科 that would be unfortunate...

Thanks!!!


Example & for easy 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].*'"];


MATCHES will let you use regular expressions.


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

For example, you can't use it for request to sqlite database to get these arrays or you will get the following exception:

Unimplemented SQL generation for predicate (SELF MATCHES ...
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜