将2个数组合并为一个数组

时间:2014-03-12 00:32:26

标签: ios objective-c arrays restkit

我正在使用RestKit来获得2个独立的API。我得到API就好了,但我需要将两个array合并为一个array

我该怎么做?这是我的代码,将根据需要发布额外的内容,谢谢! NSMutableArray *arrayarrayhArrayiArray的组合

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 个答案:

答案 0 :(得分:5)

应该像写作一样简单:

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作为临时值,你也可以避免声明这两个属性。