Remove object from NSMutableArray?

ios, nsmutablearray, nsstring, objective-c, search

Solution

You can use the following code

for (int i=0;i<[inputArray count]; i++) {
        NSString *item = [inputArray objectAtIndex:i];
        if ([item rangeOfString:s].location == NSNotFound) {
            [inputArray removeObject:item];
            i--;
        }
    }

Problem

I have an array with following elements in `ViewDidLoad` method ``` inputArray = [NSMutableArray arrayWithObjects:@"car", @"bus", @"helicopter", @"cruiz", @"bike", @"jeep", nil]; ``` I have another `UITextField` for searching the elements .So once i type some thing in `UITextField` i want to check whether that string is present in "inputArray" or not.If it is not matching with elements in inputArray then remove the corresponding elements from inputArray . ``` for (NSString* item in inputArray) { if ([item rangeOfString:s].location == NSNotFound) { [inputArray removeObjectIdenticalTo:item];//--> Shows Exception NSLog(@"Contains :%@",containsAnother); } } ``` but this code shows exception , something related to "removeobject:" Exception : ``` Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[__NSCFConstantString rangeOfString:options:range:locale:]: nil argument' *** First throw call stack: ` ```

Original source