如何比较两个NSMutableArrays? [英] How to compare two NSMutableArrays?

查看:123
本文介绍了如何比较两个NSMutableArrays?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm new to iOS development and having trouble comparing two NSMutableArray.

其中一个(代码中的 appDelegate.array )包含来自数据库的数据, c $ c> dataArray 在代码中)包含来自服务器响应的数据。

One of them (appDelegate.array in code) contains data coming from a database and another one (dataArray in code) contains data coming from a server response.

现在我要比较( dataArray )的每个元素与整个( appDelegate.array ),如果( appDelegate.array )中存在一些元素( dataArray 不做任何操作或断开,如果不存在,则将其添加到数据库中。

Now I want compare each element of (dataArray) with whole (appDelegate.array) and if some element of (dataArray) exist in (appDelegate.array) then do nothing or break and if doesn't exist then add it into database.

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

I tried, but am unable to do this. Below is the code I'm using.

提前感谢。

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

这篇关于如何比较两个NSMutableArrays?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆