Comment comparer deux NSMutableArrays?

Je suis nouveau dans le développement iOS et j'ai du mal à comparer deux NSMutableArray .

L'un d'entre eux ( appDelegate.array dans le code) contient des données provenant d'une database et un autre ( dataArray dans le code) contient des données provenant d'une réponse du server.

Maintenant, je veux comparer chaque élément de ( dataArray ) avec whole ( appDelegate.array ) et si un élément de ( dataArray ) existe dans ( appDelegate.array ) alors ne rien faire ou casser et s'il n'existe pas alors l'append dans la database.

J'ai essayé, mais je suis incapable de le faire. Voici le code que j'utilise.

Merci d'avance.

 NSMutableArray *dataArray = [responseDictionary objectForKey:@"myDATA"]; NSLog(@"%d dataArray count", [dataArray count]); for (int i = 0; i < [dataArray count]; i++) { NSLog(@"%d delegate array count",[appDelegate.array count]); NSInteger ID = [[[dataArray objectAtIndex:i] objectForKey:@"ID"] intValue]; NSSsortingng *Note = [[dataArray objectAtIndex:i] objectForKey:@"Note"]; NSSsortingng *Reminder = [[dataArray objectAtIndex:i] objectForKey:@"Reminder"]; NSInteger Status = [[[dataArray objectAtIndex:i] objectForKey:@"Completed"]intValue]; NSInteger DisplayOrder = [[[dataArray objectAtIndex:i] objectForKey:@"Display_Order"] intValue]; if ([appDelegate.array count] == 0 && Note != nil) { NSSsortingng *query = [NSSsortingng ssortingngWithFormat:@"INSERT INTO Tasks (Note, Reminder, Status, DisplayOrder) VALUES ('%@','%@','%d','%d')",Note, Reminder, Status, DisplayOrder]; //NSLog(@"%@",query); sqlite3 *database_1; NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSSsortingng *documentsDir = [documentPaths objectAtIndex:0]; NSSsortingng *database_Path = [documentsDir ssortingngByAppendingPathComponent:@"TODO.sqlite"]; if(sqlite3_open([database_Path UTF8Ssortingng], &database_1) == SQLITE_OK) { sqlite3_stmt *comstackdStatement_1; if(sqlite3_prepare_v2(database_1,[query UTF8Ssortingng], -1, &comstackdStatement_1, NULL) == SQLITE_OK) { while(sqlite3_step(comstackdStatement_1) == SQLITE_ROW) { sqlite3_step(comstackdStatement_1); } } sqlite3_finalize(comstackdStatement_1); } sqlite3_close(database_1); [self readDataFromDatabase]; } else { // NSLog(@"Entered Else"); for (int k = 0; k < [appDelegate.array count]; k++) { objectData = [appDelegate.array objectAtIndex:k]; // NSLog(@"%d",objectData.ID); NSMutableArray *IDArray = [NSMutableArray arrayWithObject:[[NSNumber numberWithChar:objectData.ID]ssortingngValue]]; NSMutableArray *NoteArray = [NSMutableArray arrayWithObject:objectData.Note]; NSMutableArray *ReminderArray = [NSMutableArray arrayWithObject:objectData.Reminder]; NSMutableArray *StatusArray = [NSMutableArray arrayWithObject:[[NSNumber numberWithChar:objectData.Status]ssortingngValue]]; NSMutableArray *DisplayOrderArray = [NSMutableArray arrayWithObject:[[NSNumber numberWithChar:objectData.DisplayOrder]ssortingngValue]]; NSLog(@"%@ server",[NSSsortingng ssortingngWithFormat:@"%d",ID]); NSLog(@"%@ database",IDArray); if ([CommonFunctions isValueInArray:[NSSsortingng ssortingngWithFormat:@"%d",ID]:IDArray] && [CommonFunctions isValueInArray:Note :NoteArray] && [CommonFunctions isValueInArray:Reminder :ReminderArray] && [CommonFunctions isValueInArray:[NSSsortingng ssortingngWithFormat:@"%d",Status] :StatusArray] && [CommonFunctions isValueInArray:[NSSsortingng ssortingngWithFormat:@"%d",DisplayOrder] :DisplayOrderArray]) { NSLog(@"Present In appDelegate.array!!!"); } else { NSSsortingng *query = [NSSsortingng ssortingngWithFormat:@"INSERT INTO Tasks (Note, Reminder, Status, DisplayOrder) VALUES ('%@','%@','%d','%d')",Note, Reminder, Status, DisplayOrder]; NSLog(@"%@",query); sqlite3 *database_1; NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSSsortingng *documentsDir = [documentPaths objectAtIndex:0]; NSSsortingng *database_Path = [documentsDir ssortingngByAppendingPathComponent:@"TODO.sqlite"]; if(sqlite3_open([database_Path UTF8Ssortingng], &database_1) == SQLITE_OK) { sqlite3_stmt *comstackdStatement_1; if(sqlite3_prepare_v2(database_1,[query UTF8Ssortingng], -1, &comstackdStatement_1, NULL) == SQLITE_OK) { while(sqlite3_step(comstackdStatement_1) == SQLITE_ROW) { sqlite3_step(comstackdStatement_1); } } sqlite3_finalize(comstackdStatement_1); } sqlite3_close(database_1); } } } 

Utilisation

 for (int i = 0; i < [dataArray count]; i++) { if ([appDelegate.array count] == 0 && Note != nil) { } else { if(![appDelegate.array containsObject:[dataArray objectAtIndex:i]]) { // Your code } else { // Do nothing Continue; or Break; Continue; } } } 
 - (BOOL) isEqual: object (ModelClassName *)
 {
     if ([self.ID isEqualToSsortingng: object.ID]) {
         returnner YES;
     }
     returnner NO;
 }

Sous-class votre object de model et substituez la méthode isEqual, puis utilisez

 [array containsObject:] méthode
 if ([array1 isEqualToArray:array2]){ // Equal.. }