我编辑的问题:- 我复制了数组。但问题是当下面的行执行 [mainRestaurantArray removeObjectAtIndex:j]; mainRestaurantArray 的索引是减少的。这将不允许删除正确的索引值
因为它显示了索引超出范围的错误。因为 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
),它是从作为 web 服务响应的数据中填充的。
中的数据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];
}
}
}
}
但是这段代码中的问题是当我删除引用丢失的值时。所以应用程序崩溃了。任何人都可以帮助我如何删除数据
提前致谢...