1

我是 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);


                }
        }

    }
4

3 回答 3

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;
    }
  }
}
于 2013-01-15T07:34:12.657 回答
0
if ([array1 isEqualToArray:array2]){

   // Equal..
}
于 2013-01-15T08:20:20.703 回答
0
- (BOOL)isEqual:(ModelClassName *)object
{
    if ([self.ID isEqualToString:object.ID]) {
        返回是;
    }
    返回否;
}

子类化您的模型对象并覆盖 isEqual 方法,然后使用

[数组包含对象:] 方法
于 2013-01-15T07:55:35.927 回答