我编辑过的问题: 我复制了数组。 但问题是当下面的行执行时 [mainRestaurantArray removeObjectAtIndex:j]; mainRestaurantArray的索引是reduce.And不允许删除正确的索引值
因为它显示索引超出范围的错误。 Becoz是arrayRestaurantList -31的总索引 和mainRestaurantArray-25的总索引(删除重复值后)。
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];
}
}
}
}
}
我有一个NSMutableArray
(mainRestaurantArray
),它是根据来自网络服务的响应数据填充的。
mainRestaurantArray
中的数据如下: -
{
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";
}
即字典形式。现在有时候数据会重复。所以我想删除它。 我编写了如下代码:
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];
}
}
}
}
但是这段代码中的问题是当我删除引用丢失的值时.. 所以应用程序崩溃了。任何人都可以帮助我如何删除数据
提前致谢...
答案 0 :(得分:5)
NSArray *cleanedArray = [[NSSet setWithArray:listItems] allObjects];
// listItems是你的NSMutableArray对象。
答案 1 :(得分:1)
您可以将此方法用于此
+(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;
}
答案 2 :(得分:0)
以下是从NSMutable Array中删除重复值的代码。。它将适合您。 myArray是你想要删除重复值的可变数组..
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];
}
}
现在打印你的数组并看到没有重复值