iphone - 如何比较两个NSMutableArray?

标签 iphone ios for-loop nsmutablearray compare

我是iOS开发的新手,无法比较两个NSMutableArray

其中一个(代码中的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);


                }
        }

    }

最佳答案


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

关于iphone - 如何比较两个NSMutableArray?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14332971/

相关文章:

php - 使用 iPhone 向 PHP 发送一些参数

iphone - 在 iPhone 操作系统上检测文件创建日期?

javascript - 我正在尝试从父容器中获取所有按钮 id,并且总是获取最后一个

iOS SwiftUI - 使用 PhotosPicker 从图库中选择图像或视频

ios - 从 iOS 应用打开 Facebook Messenger

javascript - 将字符串表示法转换为递归对象指针

c++11 - 为什么 C++11 的范围 for 循环不允许迭代映射?

android - 查找另一部智能手机的笛卡尔坐标?

iphone - 如何将 AdWhirl-banner 固定在 TabBar 上方?

ios - UIWebview 视频播放崩溃 - 仅限于 HTML 中的 jw 播放器