从数据数组中提取唯一对象

我想要在数据数组中添加名字,只有当数据数组中没有先前的名字。 当我试图打印这些名字,我确实重复。 有没有办法解决这个问题?

-(NSMutableArray *)autoComplete { NSMutableArray *dataArray = [[NSMutableArray alloc] init]; NSString *url = [NSString stringWithFormat:@"%@45.25,-95.25&limit=100&client_id=Von0J4Bu6INiez5bGby2R&client_secret=50sUjSbg7dba8cQgtpdfr5Ns7wyYTqtmKpUU3khQ",kWSURL]; NSDictionary * returnDict = (NSDictionary *) [self callWebService:url]; if([returnDict objectForKey:@"success"]) { NSArray *responceArray = [returnDict objectForKey:@"response"]; for (NSDictionary *dict in responceArray) { placeDC *place = [[placeDC alloc]init]; NSDictionary *placeDict = (NSDictionary *)[dict objectForKey:@"place" ]; NSDictionary *loctionDict =(NSDictionary *)[dict objectForKey:@"loc"]; NSString * name =[placeDict objectForKey:@"name"]; NSString * stateFull =[placeDict objectForKey:@"stateFull"]; NSString * countryFull =[placeDict objectForKey:@"countryFull"]; NSString *latitude =[loctionDict objectForKey:@"lat"]; NSString *longitude = [loctionDict objectForKey:@"long"]; place.placeNmae=name; place.countryFullName=countryFull; place.stateFullName=stateFull; NSLog(@"%@ ",stateFull); place.latitude=[latitude doubleValue]; place.longitude=[longitude doubleValue]; [dataArray addObject:place]; } } return dataArray; } 

首先检查是否有来自服务器端的任何响应,检查响应使用NSLog()或断点。

如果响应是好的,那就把下面的代码检查一下

 if (![dataArray containsObject:@"Some Name"]) { // add Object } 

您可以将名称NSString添加到NSSet并在每个循环中检查它是否包含它。

在你的内部, if你能写下类似的东西:

 NSArray *responceArray = [returnDict objectForKey:@"response"]; NSSet *names = [[NSSet alloc] init]; for (NSDictionary *dict in responceArray) { NSDictionary *placeDict = (NSDictionary *)[dict objectForKey:@"place" ]; NSString * name =[placeDict objectForKey:@"name"]; if (![names containsObject:name]) { [names addObject:name]; placeDC *place = [[placeDC alloc]init]; NSDictionary *loctionDict =(NSDictionary *)[dict objectForKey:@"loc"]; NSString * stateFull =[placeDict objectForKey:@"stateFull"]; NSString * countryFull =[placeDict objectForKey:@"countryFull"]; NSString *latitude =[loctionDict objectForKey:@"lat"]; NSString *longitude = [loctionDict objectForKey:@"long"]; place.placeNmae=name; place.countryFullName=countryFull; place.stateFullName=stateFull; NSLog(@"%@ ",stateFull); place.latitude=[latitude doubleValue]; place.longitude=[longitude doubleValue]; [dataArray addObject:place]; } } 

希望这可以帮助!

做一件事,添加你的dict在另一个数组,并在这个数组中search数据已经存在与否,

 [tempAry addObject: dict]; 

并在插入之前

  NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name == %@", name]; NSArray *filteredArray = [tempAry filteredArrayUsingPredicate:predicate]; if ([filteredArray count] == 0) { [dataArray addObject:place]; } else{ //Already exist } 

你为什么不创build一个单独的字典,作为你的阶级的一个ivar或财产,用于存储我们所需的价值,比如说:

 NSMutableDictionary *uniqueValueDict=[NSMutableDictionary new]; 

并保持您所需的价值和关键为:

 [uniqueValueDict setObject:stateFull forKey:uniqueValueDict]; 

你的工作将完成。

这是我已经应用的最简单的解决scheme,这应该让你去挑选数组中的独特元素。

  NSArray * claimedOfferArray = [[NSArray alloc]initWithObjects:@"A",@"B",@"A",@"C",@"B" nil]; NSArray * distinctArray = [[NSArray alloc]init]; distinctArray =[[NSSet setWithArray:claimedOfferArray] allObjects]; 

此代码也将与NSMutableArray工作让我知道,如果它适合你.. :)。