2017-04-20 122 views
0
NSMutableArray *arrayDetails = [[NSMutableArray alloc] init]; 
CustomClass *country1  = [[CustomClass alloc] init]; 
country1.strCountryName  = @"USA"; 
country1.code    = @"12"; 
[arrayDetails addObject:country1]; 

CustomClass *country2  = [[CustomClass alloc] init]; 
country2.strCountryName  = @"India"; 
country2.code    = @"234"; 
[arrayDetails addObject:country2]; 

CustomClass *country4  = [[CustomClass alloc] init]; 
country4.strCountryName  = @"UK"; 
country4.code    = @"34"; 
[arrayDetails addObject:country4]; 

CustomClass *country5  = [[CustomClass alloc] init]; 
country5.strCountryName  = @"USA"; 
country5.code    = @"12"; 
[arrayDetails addObject:country5]; 

CustomClass *country6  = [[CustomClass alloc] init]; 
country6.strCountryName  = @"India"; 
country6.code    = @"12"; 
[arrayDetails addObject:country6]; 

CustomClass *country7  = [[CustomClass alloc] init]; 
country7.strCountryName  = @"India"; 
country7.code    = @"12"; 
[arrayDetails addObject:country7]; 

CustomClass *country8  = [[CustomClass alloc] init]; 
country8.strCountryName  = @"USA"; 
country8.code    = @"12"; 
[arrayDetails addObject:country8]; 

CustomClass *country3  = [[CustomClass alloc] init]; 
country3.strCountryName  = @"UK"; 
country3.code    = @"12"; 
[arrayDetails addObject:country3]; 


CustomClass *country77  = [[CustomClass alloc] init]; 
country77.strCountryName  = @"PAK"; 
country77.code    = @"12"; 
[arrayDetails addObject:country3]; 

我要根據國家名,其中包含和美國在一個陣列中,印度陣列中的所有英國自定義類對象的一個​​數組,使獨立的套做套,甚至單個對象PAK。 數據來自服務器,基於我想要創建組的strContry名稱。從數組,其中包含自定義對象的數組

+0

什麼是這裏的自定義類...? –

+0

只是一個模型類 –

+0

你必須使用NSPredicate來區分自定義類中的字段。我仍然沒有代碼。但這應該是 – ajjjjjjjj

回答

0

將所有對象添加到「arraydetails」之後,請嘗試使用下面的代碼來實現相同的效果。

NSArray *countryNames = [[NSOrderedSet orderedSetWithArray:[arrayDetails valueForKey:@"strCountryName"]] array]; 
    for (int i =0; i < [countryNames count]; i++) { 
     NSMutableArray *countryArray = [[NSMutableArray alloc] init]; 
     for (CustomClass *country in arrayDetails) { 
      if ([country.strCountryName isEqualToString:[countryNames objectAtIndex:i]]) { 
       [countryArray addObject:country]; 
      } 
     } 
     NSLog(@"Country array[%d] is %@", i, countryArray); 
    } 
+0

的方式是否適合你? – QUserS

相關問題