Basic array comparison algorithm

后端 未结 4 1561
半阙折子戏
半阙折子戏 2021-01-07 08:44

I\'m trying to follow the steps found here on comparing two arrays, and knowing when to create a new object, but I just don\'t understand how it works:

<
相关标签:
4条回答
  • 2021-01-07 09:24

    Try something like this:

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

    Hope this helps!

    0 讨论(0)
  • 2021-01-07 09:25

    Something like this:

    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...
        }
    }
    
    0 讨论(0)
  • 2021-01-07 09:29

    You need to do a linear search in your objects array to check if you can find it, perhaps something like that:

    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
        }
    }
    
    0 讨论(0)
  • 2021-01-07 09:46

    I found this question after looking at the same example in the Core Data Programming Guide.

    This is my solution:

    The key is that you are walking 2 arrays separately, one array contains all the employeeId strings that need to exist in Core Data, and the other contains Employee objects that already exist in Core Data, filtered by the employeeId strings. Both arrays have been sorted.

    So lets say we have the sorted employeeIds array containing strings:

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

    And that we have a matchingEmployees array containing 2 Employee objects with employeeId 10 and 15.

    We need to create new Employee objects for employees with employeeIDs 11,12 and 20, while potentially updating the attributes of employees 10 and 15 . So:

    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++;
    }
    
    0 讨论(0)
提交回复
热议问题