1

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){
        };
    }];

1 Answer 1

5

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.

Sign up to request clarification or add additional context in comments.

10 Comments

Thanks for the response! I tried it, and it makes sense, but when I do NSLog(@"Combined array count: %lu", (unsigned long)array.count); I get "0". So its not grabbing the items for the array somehow. Any idea? The hArray and iArray definitely have items in them, so its not that. I can't tell if its some weird mapping thing with RestKit or what.
Are you sure you are correctly initializing your array property before adding objects to it? In other words: make sure your array property is not nil. You can refer to the first line under the property declarations on the example I provided above.
Are you talking about making sure I have you're first //new line of code?
Also, to add some color, when I put an NSLog(@"1st array: %d", array.count); right [array addObjectsFromArray:hArray];, nothing even gets spit out in my console for it, so obviously I'm missing something and its probably what you're talking about. I just need to make sure I'm following what your talking about
Yes. You should make sure that array is not nil when you call -addObjectsFromArray: on it.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.