1

我正在尝试按照此处找到的步骤比较两个数组,并知道何时创建一个新对象,但我只是不明白它是如何工作的:

您最终会得到两个排序的数组——一个包含传递到 fetch 请求中的员工 ID,另一个包含与它们匹配的托管对象。要处理它们,请按照以下步骤遍历排序列表:

Get the next ID and Employee. If the ID doesn't match the Employee ID, create a new Employee for that ID.
Get the next Employee: if the IDs match, move to the next ID and Employee.

不管你传入多少个 ID,你只执行一次 fetch,其余的只是遍历结果集。

基本上发生的事情是我有一个来自外部源的对象 id 数组,而客户端系统只有这些 id 表示的对象的一个​​子集。我需要弄清楚我已经拥有哪些对象,如果没有,就一个一个地创建它们。

我不明白这是如何工作的。我无法将其转换为代码:

for (int i =0;i<ids.count;i++) {
    currentId = [ids objectAtIndex:i];
    currentObject = [objects objectAtIndex:i];

    if(currentObject.id != currentId) {
        //create new object
    }

    //"get the next employee"
    //uh what?
    nextEmployee = [objects objectAtIndex:i+1]; //?
    if(nextEmployee.id == currentId) {
        //"move on to the next id"
        continue;
    }
}

我不明白这将如何工作?我错过了什么?

4

4 回答 4

1

在查看 Core Data Programming Guide 中的相同示例后,我发现了这个问题。

这是我的解决方案:

关键是你分别走2个数组,一个数组包含Core Data中需要存在的所有employeeId字符串,另一个包含Core Data中已经存在的Employee对象,通过employeeId字符串过滤。两个数组都已排序。

假设我们有一个包含字符串的已排序的employeeIds 数组:

@"10",@"11",@"12",@"15",@"20"

我们有一个matchingEmployees 数组,其中包含2 个Employee 对象,其employeeId 为10 和15。

我们需要为员工 ID 为 11,12 和 20 的员工创建新的 Employee 对象,同时可能更新员工 10 和 15 的属性。所以:

int i = 0; // employeeIds array index
int j = 0; // matchingEmployees array index

while ((i < [employeeIds count]) && (j <= [matchingEmployees count])){

  NSString *employeeId = [employeeIds objectAtIndex:i];

  Employee *employee = nil;

  if ([matchingEmployees count]!=0)
      employee = [matchingEmployees objectAtIndex:j];

  if (![employeeId isEqualToString:employee.employeeId]){

      employee = //Insert new Employee entity into context
      employee.employeeId = employeeId;

      //Set any attributes for employee that do not change
  }
  else {
      //We matched employeeId to Employee so the next iteration
      //of this loop should check the next Employee object
      j++; 
  }

  //Set any attributes for employee that change with each update

  i++;
}
于 2012-09-25T22:03:31.497 回答
0

您需要在对象数组中进行线性搜索以检查是否可以找到它,可能是这样的:

for (int i = 0; i < ids.count; i++) {
    bool found = NO;
    currentId = [ids objectAtIndex:i];

    // We need to traverse the whole array to check if we can find the objectID somewhere...
    for(int j = 0; j < objects.count; j++) {
        currentObject = [objects objectAtIndex:j];

        if (currentId == currentObject) {
            found = YES;
            break;
        }
    }

    if (!found)
    {
        // Create the new object
    }
}
于 2012-06-25T03:37:30.843 回答
0

像这样的东西:

NSArray *wholeList = [[NSArray alloc]initWithObjects:@"employee1", @"employee2", @"employee3", @"employee4", @"employee5",@"employee6", nil];
NSArray *partialList = [[NSArray alloc]initWithObjects:@"employee2", @"employee13", @"employee7", nil];

for (id employeeID in partialList)  //get one employee from the partialList
{
    if (![wholeList containsObject:employeeID])  //check to see if it is in the wholeList
    {
        NSLog(@"This employee is not in the wholeList: %@", employeeID);
        // do create new employee object for this employeeID, whatever...
    }
}
于 2012-06-25T03:38:43.653 回答
0

尝试这样的事情:

//Employee.h
@property (nonatomic) NSInteger ID;
@property (nonatomic, strong) NSString *name;

//Employee.m
@synthesize ID, name;



//Place where you put algorithm
#import "Employee.h"
------------------------
NSMutableArray *employeeIDs = [NSArray arrayWithObjects:[NSNumber numberWithInt:123], [NSNumber numberWithInt:456], [NSNumber numberWithInt:789], nil];

//Creates employee objects
Employee *employee1 = [[Employee alloc] init];
employee1.ID = 123;
employee1.name = @"John Smith";

Employee *employee2 = [[Employee alloc] init];
employee2.ID = 456;
employee2.name = @"Bob Day";

Employee *employee3 = [[Employee alloc] init];
employee3.ID = 789;
employee3.name = @"Steve Jobs";

NSMutableArray *employeesArray = [NSArray arrayWithObjects:employee1, employee2, employee3, nil];

for (int index = 0; index <= [employeeIDs count]; index++) {

    for(id currentEmployee in employeesArray){

        if(currentEmployee.ID != currentID){

            Employee *newEmployee = [[Employee alloc] init];
            newEmployee.name = [NSString stringWithFormat:@"Employee Name"];
            newEmployee.ID = 384;

            [employeeIDs addObject:[NSNumber numberWithInteger:newEmployee.ID]];
            [employees addObject:newEmployee.name];

        }
    }
}

希望这可以帮助!

于 2012-06-25T03:49:57.020 回答