我正在尝试跟踪steps found here比较两个数组,并知道何时创建一个新对象,但我只是不明白它是如何工作的:
最终得到两个已排序的数组 - 一个传递了员工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,都只执行一个 获取,其余的只是走结果集。
基本上发生的事情是我有一个来自外部源的对象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;
}
}
我看不出那会怎么样?我错过了什么?
答案 0 :(得分:1)
在查看“核心数据编程指南”中的相同示例后,我发现了这个问题。
这是我的解决方案:
关键是你分别走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++;
}
答案 1 :(得分: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
}
}
答案 2 :(得分: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...
}
}
答案 3 :(得分: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];
}
}
}
希望这有帮助!