获取一个的NSArray [英] Get one NSArray

查看:123
本文介绍了获取一个的NSArray的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道如何为合并两个的阵列到一个阵列

I was wondering how to combine two array's into one array.

我要合并的tableView 来显示最近的。

I want the combined tableView to show the most recent.

我将张贴任何额外的code或有助于在必要时,感谢这么多!

I will post any extra code or help as necessary, thanks so much!

推荐答案

中这个问题的复杂性的原因是,该数据是从两个不同的源异步到来。让我们周围的武器,我们已经得到控制的问题。我的建议不同之处在于它的目的是照顾多个非同步源向右走,留下简单的code在所有其他领域的人。

The cause of the complexity in this question is that the data is coming from two different sources asynchronously. Get our arms around that and we've got the problem under control. My suggestion differs from the others in that it aims to take care of the multiple asynch sources right away, leaving simpler code in all the other areas.

要处理两个非同步源的方法是使用一个嵌套完成序列化。在这里,我只是把你贴code和分解为两个方法,每个API。每一个需要成功和失败块匹配对象管理器的界面......

The way to handle two asynch sources is to serialize them with a nested completion. Here, I just took your posted code and factored it into two methods, one for each api. Each takes a success and failure block matching the object manager's interface...

- (void)loadOneWithSuccess:(void (^)(RKObjectRequestOperation *operation, RKMappingResult *mappingResult))success
                   failure:(void (^)(RKObjectRequestOperation *operation, NSError *error))failure {

    NSString *apikey = @kCLIENTKEY;
    NSDictionary *queryParams = @{@"apikey" : apikey};
    NSString *path = [NSString stringWithFormat:@"v1/n/?limit=4&leafs=%@&themes=%@", leafAbbreviation, themeID];

    [self.eObjectManager getObjectsAtPath:path parameters:queryParams success:success failure:failure];
}

- (void)loadTwoWithSuccess:(void (^)(RKObjectRequestOperation *operation, RKMappingResult *mappingResult))success
                   failure:(void (^)(RKObjectRequestOperation *operation, NSError *error))failure {

    NSString *path = @"v1/u/2/m/recent/?client_id=e999";
    [self.iObjectManager getObjectsAtPath:path parameters:nil success:success failure:failure];
}

现在我们可以做loadMedia我们所需要的,这是从每个API装载,合并和排序作为一个单一的模式。声明称为的NSMutableArray 属性 combinedModel 。其他答案建议将此作为tableDataList或contentArray。在我的建议的主要区别是照顾组合作为的一部分组合获取。

Now we can make loadMedia do what we need, which is to load from each api, combine and sort as a single model. Declare an NSMutableArray property called combinedModel. Other answers have suggested this as tableDataList or contentArray. The key difference in my suggestion is to take care of the combination as part of a combined fetch.

- (void)loadMedia {

    self.combinedModel = [NSMutableArray array];

    [self loadOneWithSuccess:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {

        [self.combinedModel addObjectsFromArray:mappingResult];

        // here's the trick.  call API2 here.  Doing so will serialize these two requests
        [self loadTwoWithSuccess:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {

            [self.combinedModel addObjectsFromArray:mappingResult];
            [self sortCombinedModel];
            [self.tableView reloadData];

        } failure:^(RKObjectRequestOperation *operation, NSError *error) {
            NSLog(@"No?: %@", error);
        }];

    } failure:^(RKObjectRequestOperation *operation, NSError *error) {
        NSLog(@"No?: %@", error);
    }];
}

现在只有两个问题依然存在(1)排序异种对象的数组,(2)在表视图呈现异质的对象。第一类:

Now only two problems remain (1) sorting an array of heterogenous objects, (2) rendering heterogenous objects in a table view. First sort:

- (void)sortCombinedModel {
    [self.combinedModel sortUsingComparator:^NSComparisonResult(id a, id b) {
        NSDate *dateA, *dateB;
        dateA = ([a isKindOfClass:[Feed self]])? ((Feed *)a).published : ((Data *)a).created_time;
        dateB = ([b isKindOfClass:[Feed self]])? ((Feed *)b).published : ((Data *)b).created_time;
        return [dateA compare:dateB];
    }];
}

现在的表, self.combinedModel 为表视图的新模式。所有数据源的方法应参考。的cellForRowAtIndexPath:应该表现就像排序:。简而言之...

Now for the table, self.combinedModel is the new model for the table view. All the datasource methods should refer to it. cellForRowAtIndexPath: should behave just like the sort:. In a nutshell...

id model = self.combinedModel[indexPath.row];
if ([model isKindOfClass:[Feed self]) {
    Feed *feed = (Feed *)model;
    // configure cell with feed
} else {
    Data *data = (Data *)model;
    // configure cell with data
}

这篇关于获取一个的NSArray的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆