基本的数组比较algorithm

我试图按照这里find的步骤比较两个数组,并知道什么时候创build一个新的对象,但我只是不明白它是如何工作的:

您最终得到两个sorting数组 – 一个是传递给获取请求的员工ID,另一个是与他们匹配的pipe理对象。 要处理它们,请按以下步骤执行sorting的列表:

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. 

无论您传入多less个ID,您只执行一次提取,其余的只是执行结果集。

基本上发生了什么事情是我有一个来自外部来源的对象ID数组,客户端系统只有这些ID表示的对象的子集。 我需要弄清楚我已经拥有了哪些物品,如果我没有这些物品,请逐个创build它们。

我不明白这是如何工作的。 我很难将其翻译成代码:

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

我不明白这是怎么回事? 我错过了什么?

尝试这样的事情:

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

希望这可以帮助!

在查看“核心数据编程指南”中的同一个例子后,我发现了这个问题。

这是我的解决scheme:

关键是你分别走两个数组,一个数组包含所有需要存在于Core Data中的employeeIdstring,另一个数组包含已经存在于Core Data中的Employee对象,由employeeIdstring进行过滤。 这两个数组已被sorting。

所以,让我们说我们有sorting的employeeIds数组包含string:

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

而且我们有一个包含employeeId为10和15的两个Employee对象的matchingEmployees数组。

我们需要为employeeID为11,12和20的雇员创build新的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++; } 

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

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

像这样的东西:

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