如何根据索引从数据结构中获取特定对象

How to get a specific object from data structure based on Index

我正在创建一个使用自定义设置的自定义 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。但在 'real life' 中,它可能是 0-10。

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

例如,如果索引为 1,我想获取 NSDictionary:

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

我不确定的部分是,在整个 NSDictionary 数据中,我有子 NSArray 的对象数量不同。

获取字典的键:

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];

...应该给你:

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