Saturday, June 1, 2013

NSMutableArray removeObject example in Objective C (iOS).


NSMutableArray removeObject

Removes all occurrences in the array of a given object.

- (void)removeObject:(id)anObject

Parameters
anObject
The object to remove from the array.

Discussion of [NSMutableArray removeObject]
This method uses indexOfObject: to locate matches and then removes them by using removeObjectAtIndex:. Thus, matches are determined on the basis of an object’s response to the isEqual: message. If the array does not contain anObject, the method has no effect (although it does incur the overhead of searching the contents).

NSMutableArray removeObject example.
NSMutableArray *myArray;
NSArray *myArrayCopy = [NSArray arrayWithArray:myArray];

for (NSObject *anObject in myArrayCopy) {
if (shouldRemove(anObject)) {
[myArray removeObject:anObject];
}
}

Example of [NSMutableArray removeObject].
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle != UITableViewCellEditingStyleDelete)
        return;

    NSString *searchString = [self.mruSearchItems objectAtIndex:indexPath.row];
    [self.mruItems removeObject:searchString];
    [self.searchTableView reloadData];
}

NSMutableArray removeObject example.
for(id item in items) {
    if([item isEqual:itemToDelete]) {
        [items removeObject:item];
        break;
    }
}

End of NSMutableArray removeObject example article.