Reputation: 3473
My edited Question:- I had copied the array. But the problem is when the below line executes [mainRestaurantArray removeObjectAtIndex:j]; the index of the mainRestaurantArray is reduces.And which will not allow to remove the proper indexed value
As it shows the error of index out of bound. Becoz the total index of arrayRestaurantList -31 and total index of mainRestaurantArray-25 (after removing the duplicate value).
if([arrayRestaurantList count]==[mainRestaurantArray count]){
BtnMore.hidden=YES;
TablView.frame=CGRectMake(0,57, 320, 360);
} else{
[mainRestaurantArray removeAllObjects];
mainRestaurantArray=[[NSMutableArray arrayWithArray:arrayRestaurantList] retain];
if ([arrayRestaurantList count]>10) {
for (int i=0;i<[arrayRestaurantList count]-10;i++) {
NSString *Old_data;
Old_data=[[arrayRestaurantList objectAtIndex:i] objectForKey:@"Name"];
NSLog(@"olddata==>%@",Old_data);
for (int j=[arrayRestaurantList count]-10;j<[arrayRestaurantList count];j++) {
NSString *New_data;
New_data=[[arrayRestaurantList objectAtIndex:j] objectForKey:@"Name"];
NSLog(@"olddata==>%@ newdata==>%@",Old_data,New_data);
if ([New_data isEqualToString:Old_data]) {
//BtnMore.hidden=YES;
[mainRestaurantArray removeObjectAtIndex:j];
}
}
}
}
}
I have a NSMutableArray
(mainRestaurantArray
) which is filled from the data which is coming as a response from webservice.
The data in mainRestaurantArray
are like:-
{
Address = "Ellisbridge Opposite Parimal Garden Near Doctor House Ahmedabad Gujarat 380006 India (Collegian Restaurant)";
Image = "";
Latitude = "23.020119999999999";
Longitude = "72.556343999999996";
Name = "Collegian Restaurant";
"Phone_Number" = "079 26401020";
url = "";
}
{
Address = "Ground Floor Aakash Ganga Ellisbridge B/H Gujrat College : Next To Art Palace Ellisbridge Ahmedabad Gujarat 380006 India (TC's Bread Shop)";
Image = "";
Latitude = "23.023137999999999";
Longitude = "72.560096000000001";
Name = "TC's Bread Shop";
"Phone_Number" = "079 26568158";
url = "";
}
{
Address = "Center Point Panchwati Crossing C G Road Ahmedabad Gujarat 380009 India (Kings Food Factory)";
Image = "";
Latitude = "23.022670000000002";
Longitude = "72.555651999999995";
Name = "Kings Food Factory";
"Phone_Number" = "079 4002 1122";
url = "http://www.kingsfoodfactory.com";
}
i.e in dictionary form.Now some time it happens that the data get repeated.So i want to remove it. I had written the code like below:
if ([mainRestaurantArray count]>10) {
for (int i=0;i<[mainRestaurantArray count]-10;i++) {
NSString *Old_data;
Old_data=[[mainRestaurantArray objectAtIndex:i] objectForKey:@"Name"];
NSLog(@"olddata==>%@",Old_data);
for (int j=[mainRestaurantArray count]-10;j<[mainRestaurantArray count];j++) {
NSString *New_data;
New_data=[[mainRestaurantArray objectAtIndex:j] objectForKey:@"Name"];
NSLog(@"olddata==>%@ newdata==>%@",Old_data,New_data);
if ([New_data isEqualToString:Old_data]) {
BtnMore.hidden=YES;
TablView.frame=CGRectMake(0,57, 320, 360);
[mainRestaurantArray removeObjectAtIndex:j];
}
}
}
}
But the problem in this code is when i remove the value the reference get lost.. So application get crashed.Can anybody help me how to get remove the data
Thanks in advance...
Upvotes: 0
Views: 1308
Reputation: 3979
You can use this method for this
+(NSMutableArray*)GetFilterData :(NSMutableArray*)array;
{
//Filter duplicate data
NSArray *copy = [array copy];
NSInteger index = [copy count] - 1;
for (id object in [copy reverseObjectEnumerator]) {
if ([array indexOfObject:object inRange:NSMakeRange(0, index)] != NSNotFound) {
[array removeObjectAtIndex:index];
}
index--;
}
[copy release];
NSLog(@"arr %@",[array description]);
return array;
}
Upvotes: 1
Reputation: 97
Here is the code of removing duplicates values from NSMutable Array..it will work for you. myArray is your Mutable Array that you want to remove duplicates values..
for(int j = 0; j < [myArray count]; j++){
for( k = j+1;k < [myArray count];k++){
NSString *str1 = [myArray objectAtIndex:j];
NSString *str2 = [myArray objectAtIndex:k];
if([str1 isEqualToString:str2])
[myArray removeObjectAtIndex:k];
}
}
Now print your array and will see there is no repeated value
Upvotes: 0
Reputation: 2218
NSArray *cleanedArray = [[NSSet setWithArray:listItems] allObjects];
//listItems is your NSMutableArray object.
Upvotes: 5