I would like to filter NSMutableArray elements that contains "some" string. ListArchives is filled with string elements and listFiles must be a filtered one. XCode generates an alert at last posted line. What am doing wrong? any other method to filter elements of NSMutableArray?
NSString *match = @"*some*";
listFiles = [[NSMutableArray alloc] init];
NSPredicate *sPredicate = [NSPredicate predicateWithFormat:@"SELF like[cd] %@", match];
listFiles = [listArchives filteredArrayUsingPredicate:sPredicate];
Try using contains
instead of like
, for example as follows:
NSString *match = @"some";
listFiles = [[NSMutableArray alloc] init];
NSPredicate *sPredicate = [NSPredicate predicateWithFormat:@"SELF CONTAINS[cd] %@", match];
listFiles = [[listArchives filteredArrayUsingPredicate:sPredicate] mutableCopy];
If you want to search as 'like' operator. Lets say you have a NSArray with following contents :
static int count = 0;
NSArray *results = [NSArray arrayWithObjects:@"What was", @"What is", @"What will", nil];
NSString *targetString = @"What"
for (NSString *strObj in results)
{
if ([strObj rangeOfString:targetString].location != NSNotFound){
NSLog (@"Found: %@", strObj);
count = count + 1;
}
}
NSLog(@"There were %d occurence of %@ string",count,targetString);
NSString *match = @"some";
listFiles = [[NSMutableArray alloc] init];
NSPredicate *sPredicate = [NSPredicate predicateWithFormat:@"SELF BEGINSWITH[c] %@", match];
listFiles = [listArchives filteredArrayUsingPredicate:sPredicate];
For robust searching this can serve you as some what "Like" serve in SQL
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.