2013-01-15 144 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