0

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

3 Answers 3

4

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];
Sign up to request clarification or add additional context in comments.

4 Comments

ok, seems better option. However still showing an alert, "Incomptaible pointer types assigning to NSMutableArray _strong from NSArray". So if I declare listFiles as NSArray solves it but no other way to get listFiles also mutable?
@Jaume This is because the filtered array is not known to the compiler to be an NSMutableArray. Try this instead of the last line: [listFiles setArray:[listArchives filteredArrayUsingPredicate:sPredicate]];
last line listFiles = [[listArchives filteredArrayUsingPredicate:sPredicate] mutableCopy]
@Nazir Thanks! You can always suggest an edit - it's often faster than writing a comment.
2

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

Comments

0
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.