Search code examples
iosobjective-carraysrestkit

Combining 2 arrays into one array


I'm using RestKit to get 2 separate API's. I am getting the API's just fine, but I need to combine the two arrays into one array.

How would I do this? Here is my code, will post extra as needed, thanks! (NSMutableArray *array is the array that will be the combination of hArray and iArray)

ViewController.m

@property (strong, nonatomic) NSArray *hArray;
@property (strong, nonatomic) NSMutableArray *array;
@property (strong, nonatomic) NSArray *iArray;


[[RKObjectManager sharedManager] loadObjectsAtResourcePath:
[NSString stringWithFormat:@"/n/?limit=200&l=%@&t=%@&apikey=111", 
lAbbreviation, tID] usingBlock:^(RKObjectLoader *loader) {
        loader.onDidLoadObjects = ^(NSArray *objects){
            hArray = objects;
            [_tableView reloadData];
        };
        [loader.mappingProvider setMapping:[F mapping] forKeyPath:@"f"];
        loader.onDidLoadResponse = ^(RKResponse *response){
        };
    }];

    [self.iObjectManager loadObjectsAtResourcePath:
    [NSString stringWithFormat:@"/u/?client_id=111"] 
    usingBlock:^(RKObjectLoader *loader) {
        loader.onDidLoadObjects = ^(NSArray *oI){
            iArray = oI;
            [_tableView reloadData];
        };
        [loader.mappingProvider setMapping:[Data mapping] forKeyPath:@"data"];
        loader.onDidLoadResponse = ^(RKResponse *response){
        };
    }];

Solution

  • It should be as simple as writing:

    array = [NSMutableArray array];
    [array addObjectsFromArray:hArray];
    [array addObjectsFromArray:iArray];
    

    To be more specific with your example, here's how you should edit your code:

    @property (strong, nonatomic) NSArray *hArray;
    @property (strong, nonatomic) NSMutableArray *array;
    @property (strong, nonatomic) NSArray *iArray;
    
    array = [NSMutableArray array]; // new line
    
    [[RKObjectManager sharedManager] loadObjectsAtResourcePath:
    [NSString stringWithFormat:@"/n/?limit=200&l=%@&t=%@&apikey=111", 
    lAbbreviation, tID] usingBlock:^(RKObjectLoader *loader) {
        loader.onDidLoadObjects = ^(NSArray *objects){
            hArray = objects;
            [array addObjectsFromArray:hArray]; // new line
            [_tableView reloadData];
        };
        [loader.mappingProvider setMapping:[F mapping] forKeyPath:@"f"];
        loader.onDidLoadResponse = ^(RKResponse *response){
        };
    }];
    
    [self.iObjectManager loadObjectsAtResourcePath:
    [NSString stringWithFormat:@"/u/?client_id=111"] 
    usingBlock:^(RKObjectLoader *loader) {
        loader.onDidLoadObjects = ^(NSArray *oI){
            iArray = oI;
            [array addObjectsFromArray:iArray]; // new line
            [_tableView reloadData];
        };
        [loader.mappingProvider setMapping:[Data mapping] forKeyPath:@"data"];
        loader.onDidLoadResponse = ^(RKResponse *response){
        };
    }];
    

    For clarity, I used the // new line comment in each line I added.

    NOTE: if you use iArray and hArray just as temporary values, you could also avoid declaring the two properties.