Friday, September 28, 2012

Findout & Remove a particular object from an array of object.



This little snippet show how to find out a particular object from an array of object.

NSArray *array = [NSArray arrayWithObjects: @"katrina", @"aishwarya", @"bipasha", @"karina", nil];

for (NSString *element in array) {
if ([element isEqualToString:@"katrina"]) {
NSLog(@”Got katrina,no need for searching any more”);
break;

}

}

Also how to remove duplication from a NSMutableArray

NSArray *copy = [favouriteArray copy];
NSInteger index = [copy count] - 1;
for (id object in [copy reverseObjectEnumerator]) {
if ([favouriteArray indexOfObject:object inRange:NSMakeRange(0, index)] !=NSNotFound) {
[favouriteArray removeObjectAtIndex:index];
}
index--;
}
[copy release];

No comments:

Post a Comment