2013-01-15 137 views
1

我是新来的iOS开发和遇到问题比较两个NSMutableArray如何比较两个NSMutableArrays?

其中之一(在appDelegate.array代码)包含数据从数据库和另一个(dataArray在代码)来包含数据从一个服务器响应到来。

现在我想的(dataArray)的每个元素与全(appDelegate.array)比较和如果(dataArray)的一些元素(appDelegate.array)存在,则什么也不做或断裂,如果不存在,然后将其添加到数据库中。

我试过,但我无法做到这一点。以下是我正在使用的代码。

预先感谢您。

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]; 
     NSString *Note = [[dataArray objectAtIndex:i] objectForKey:@"Note"]; 
     NSString *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) 
     { 
      NSString *query = [NSString stringWithFormat:@"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); 
      NSString *documentsDir = [documentPaths objectAtIndex:0]; 
      NSString *database_Path = [documentsDir stringByAppendingPathComponent:@"TODO.sqlite"]; 

      if(sqlite3_open([database_Path UTF8String], &database_1) == SQLITE_OK) 
      { 
       sqlite3_stmt *compiledStatement_1; 
       if(sqlite3_prepare_v2(database_1,[query UTF8String], -1, &compiledStatement_1, NULL) == SQLITE_OK) 
       { 
        while(sqlite3_step(compiledStatement_1) == SQLITE_ROW) 
        { 
         sqlite3_step(compiledStatement_1); 
        } 
       } 
       sqlite3_finalize(compiledStatement_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]stringValue]]; 
       NSMutableArray *NoteArray = [NSMutableArray arrayWithObject:objectData.Note]; 
       NSMutableArray *ReminderArray = [NSMutableArray arrayWithObject:objectData.Reminder]; 
       NSMutableArray *StatusArray = [NSMutableArray arrayWithObject:[[NSNumber numberWithChar:objectData.Status]stringValue]]; 
       NSMutableArray *DisplayOrderArray = [NSMutableArray arrayWithObject:[[NSNumber numberWithChar:objectData.DisplayOrder]stringValue]]; 

       NSLog(@"%@ server",[NSString stringWithFormat:@"%d",ID]); 
       NSLog(@"%@ database",IDArray); 

       if ([CommonFunctions isValueInArray:[NSString stringWithFormat:@"%d",ID]:IDArray] && [CommonFunctions isValueInArray:Note :NoteArray] && [CommonFunctions isValueInArray:Reminder :ReminderArray] && [CommonFunctions isValueInArray:[NSString stringWithFormat:@"%d",Status] :StatusArray] && [CommonFunctions isValueInArray:[NSString stringWithFormat:@"%d",DisplayOrder] :DisplayOrderArray]) 
       { 
        NSLog(@"Present In appDelegate.array!!!"); 
       } 
       else 
       { 
        NSString *query = [NSString stringWithFormat:@"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); 
        NSString *documentsDir = [documentPaths objectAtIndex:0]; 
        NSString *database_Path = [documentsDir stringByAppendingPathComponent:@"TODO.sqlite"]; 

        if(sqlite3_open([database_Path UTF8String], &database_1) == SQLITE_OK) 
        { 
         sqlite3_stmt *compiledStatement_1; 
         if(sqlite3_prepare_v2(database_1,[query UTF8String], -1, &compiledStatement_1, NULL) == SQLITE_OK) 
         { 
          while(sqlite3_step(compiledStatement_1) == SQLITE_ROW) 
          { 
           sqlite3_step(compiledStatement_1); 
          } 
         } 
         sqlite3_finalize(compiledStatement_1); 
        } 
        sqlite3_close(database_1); 


       } 
     } 

    } 
+0

看到http://stackoverflow.com/questions/6137469/how-to-compare-two-nsmutablearray –

+0

你的代码似乎是正确的。我可能会犯一些小错误。你能告诉我们究竟发生了什么问题吗? – Rushi

+1

ID是唯一的吗?如果是这样,你可以在appDelegate.array上做一个谓词来查找所有不在其他数组中的元素。也就是说,“获取ID不存在于数组中的appDelegate.array中的所有元素”。看看NSPredicate,也许是块基的方法。现在,迭代已过滤的数组并存储该数组中的所有元素。 – simonbs

回答

7

使用

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; 
    } 
    } 
} 
+0

@roamingsoul:试试吧!它适用于你的情况。 – Nayan

+0

@roamingsoul我不明白..你在for循环中写了这个条件吗?如果条件失败和appDelegate.array包含对象继续下一个对象的循环.. –

+0

@roamingsoul检查我的更新代码.. –

0
 
- (BOOL)isEqual:(ModelClassName *)object 
{ 
    if ([self.ID isEqualToString:object.ID]) { 
     return YES; 
    } 
    return NO; 
} 

子类的模型对象和重载isEqual方法,然后用

 
[array containsObject:] method 
0
if ([array1 isEqualToArray:array2]){ 

    // Equal.. 
} 
+0

为什么不直接使用'[array1 isEqualToArray:array2]'?在另一种方法中使用它有什么意义?我认为OP正在寻找Anusha发布的上述答案。 – iDev

+0

我只是想表明它的工作原理与mutableArray :) – junaidsidhu

+0

Anwser编辑现在 – junaidsidhu