Saturday, June 1, 2013

NSMutableArray removeObjectAtIndex example in Objective C (iOS).


NSMutableArray removeObjectAtIndex

Removes the object at index .

- (void)removeObjectAtIndex:(NSUInteger)index

Parameters
index
The index from which to remove the object in the array. The value must not exceed the bounds of the array.
Important: Raises an NSRangeException if index is beyond the end of the array.

Discussion of [NSMutableArray removeObjectAtIndex]
To fill the gap, all elements beyond index are moved by subtracting 1 from their index.

NSMutableArray removeObjectAtIndex example.
    NSMutableArray *mArray = [[NSMutableArray alloc]init];
    [mArray addObject:@"one"];
    [mArray addObject:@"two"];
    [mArray addObject:@"three"];

    [mArray removeObjectAtIndex:0];

    NSLog(@"mArray at 0: %@",[mArray objectAtIndex:0]);

Example of [NSMutableArray removeObjectAtIndex].
if (indexPath.row>=0 && indexPath.row     [bookmarkCollection removeObjectAtIndex: indexPath.row];
} else {
    NSLog(@"indexPath.row is out of boundry of bookmarkCellection size: %d", bookmarkCollection.count);
}

NSMutableArray removeObjectAtIndex example.
NSMutableArray *sampleArray = [[NSMutableArray alloc]
                         initWithObjects:@"1",@"2",@"3",@"4", nil];
NSLog(@"%@", sampleArray);
[sampleArray removeObjectAtIndex:0];
NSLog(@"%@", sampleArray);

End of NSMutableArray removeObjectAtIndex example article.