NSPredicate filter by first letter that is contained in an array

I have an array of strings:

@[@"ballot", @"1-time", @"32marks", @"zoo"];

I need a predicate that finds all the strings that start with a number. So the filtered array should be:

@[@"1-time", @"32marks"]

Here's what I'm trying so far:

data = @[@"ballot", @"1-time", @"32marks", @"zoo"];
NSArray *numbers = @[@"0", @"1", @"2", @"3", @"4", @"5",@"6", @"7"];
NSPredicate *firstPredicate = [NSPredicate predicateWithFormat:@"ANY %K IN %@", numbers];
NSPredicate *secondPredicate = [NSPredicate predicateWithFormat:@"SELF BEGINSWITH"];

NSCompoundPredicate *predicate = [NSCompoundPredicate andPredicateWithSubpredicates:
                                      @[firstPredicate, secondPredicate]];

data = [data filteredArrayUsingPredicate:predicate];

It crashes with:

-[__NSArrayI rangeOfString:]: unrecognized selector sent to instance 0x15fc99a0

I don't think I need a compound predicate but I can't figure out how to format 'numbers' into the predicate string so that it chooses any number string in 'numbers'. Thanks.


You can just pass a simple regex to your predicate to match any strings that begin with a number. Something like:

NSArray *data = @[@"ballot", @"1-time", @"32marks", @"zoo"];

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", @"^\\d.+"];
// Or ^\\d(.+)? if you want to match single-digit numbers also

data = [data filteredArrayUsingPredicate:predicate];

NSLog(@"%@", data); // Outputs: ("1-time", 32marks)

Need Your Help

InvalidPluginExecutionException dialog does not show up - CRM 2011

dynamics-crm-2011 dynamics-crm crm

I have a Plugin on the Creation, Updating, and Deletion of the OpportunityProduct entity in the CRM 2011. I want to through exception on the success of some operations, means want to display dialog...

Permission to schedule posts on Facebook wall using JavaScript API

javascript facebook

I'm trying to schedule a post on a Facebook wall, but I just get an error like 'you do not have permission to schedule posts'.