我正在使用RestKit获得2个单独的API。我得到的API很好,但是我需要将两个array组合成一个array

我该怎么做?这是我的代码,需要时会额外发布,谢谢!
(NSMutableArray *arrayarray,它将是hArrayiArray的组合)

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

最佳答案

它应该像编写一样简单:

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

为了更具体地说明您的示例,请按以下步骤编辑代码:
@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){
    };
}];

为了清楚起见,我在添加的每一行中使用了// new line注释。

注意:如果仅将iArray和hArray用作临时值,则还可以避免声明两个属性。

08-26 03:19