如何从基于Index的数据结构中获取特定对象

时间:2015-06-10 22:02:25

标签: ios objective-c nsarray nsdictionary

我正在创建一个使用自定义设置的自定义UIPageViewController。我拥有的数据结构解析为以下格式:

{
    "1.0" =     (
                {
            detail = "Detail 0";
            icon = "Pull to Refresh";
            title = "Item 0";
        },
                {
            detail = "More easily refresh a subscription or playlist.";
            icon = "Pull to Refresh";
            title = "Pull to Refresh";
        }
    );
    "1.1" =     (
                {
            detail = "Create custom stations of your favorite podcasts.";
            icon = Stations;
            title = "Custom Stations";
}

当为页面视图创建每个子内容视图控制器时,我有索引,在上面的例子中它将是0,1,2。但在现实生活中#39;例如,它可能是0-10。

如何从索引中获取相关的子字典引用?

举个例子,如果索引是1,我想得到NSDictionary:

{
            detail = "More easily refresh a subscription or playlist.";
            icon = "Pull to Refresh";
            title = "Pull to Refresh";
}

我不确定的部分是,在NSDictionary个数据的总体NSArray内,我有belongs_to个子对象的不同数量。

1 个答案:

答案 0 :(得分:1)

获取字典的键:

schema.pre('save', function(next) {
  var self = this;
  // Save the topic
  neo.query('MERGE (t:Topic { title: {title}, aliases: {aliases}, type: {type}, mongoId: {_id} }) RETURN id(t) as id;', this, function(err, data) {
    self.neoId = data['id'];
    next(err);
  });
});

通过将密钥视为版本号来对此数组进行排序,因为它们看起来就是这样(基于您的数据结构格式):

NSArray *keys = yourMainDictionary.allKeys;

迭代这些键并将每个数组的子词典添加到一个可变数组中:

NSArray *sortedKeys = [keys sortedArrayUsingComparator:^(NSString *key1, NSString *key2) {
    return [key1 compare:key2 options:NSNumericSearch];
}];

您现在拥有一个名为NSMutableArray *allDictionaries = [NSMutableArray array]; for (NSString *key in sortedKeys) { NSArray *subDictionaries = yourMainDictionary[key]; [allDictionaries addObjectsFromArray:subDictionaries]; } 的所有子词典的数组。因此,如果您有索引,则只需像访问其他任何数组一样访问它,例如:

allDictionaries

......应该给你:

NSDictionary *dictionaryWithIndex1 = allDictionaries[1];