Jaume Jaume - 4 months ago 26
iOS Question

xcode filter NSMutableArray elements by string contents

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];

Answer

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];