ios 通过索引获取 NSDictionary 键?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23658209/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 00:08:24  来源:igfitidea点击:

Getting NSDictionary key by index?

iosobjective-cios7nsdictionaryuipickerview

提问by Sam Luther

Is it possible to get a dictionary key by index?

是否可以通过索引获取字典键?

I'm trying to use a dictionary for the datasource of a picker view and want to access it in following delegate method:

我正在尝试将字典用于选择器视图的数据源,并希望通过以下委托方法访问它:

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
if (component == 0){
   return  [NSString stringWithFormat:@"%@",[myDict // how to get the key]];
}

EDIT--

编辑 -

myDict is something like:

myDict 是这样的:

Item 1

第 1 项

  • sub item 1
  • sub item 2
  • 分项 1
  • 分项 2

Item 2

第 2 项

  • sub item 1
  • sub item 2
  • 分项 1
  • 分项 2

I want to use myDict as the datasource for a picker view that has 2 sections:

我想使用 myDict 作为具有 2 个部分的选择器视图的数据源:

section 0 = myDict keys (Item 1, Item 2)

第 0 部分 = myDict 键(第 1 项,第 2 项)

section 1 = corresponding myDict values for selected section 0 row (sub item 1, sub item 2)

第 1 节 = 所选第 0 节行(子项 1、子项 2)的相应 myDict 值

回答by dasblinkenlight

Since NSDictionaryis an unorderedassociative container, it has no concept of indexing. Its keys are ordered arbitrarily, and that order can change in the future.

由于NSDictionary是一个无序的关联容器,它没有索引的概念。它的键是任意排序的,并且该顺序将来可以更改。

You can get an NSArrayof keys from the dictionary, and apply indexing to it:

您可以NSArray从字典中获取一个of 键,并对其应用索引:

NSArray *keys = [myDict allKeys]; // Warning: this order may change.

However, this indexing scheme would remain consistent only as long as the dictionary remains unchanged: for example, if you use NSMutableDictionary, adding an extra key may change the ordering of the existing keys. This leads to extremely hard-to-debug problems.

但是,只有在字典保持不变时,此索引方案才会保持一致:例如,如果您使用NSMutableDictionary,则添加额外的键可能会更改现有键的顺序。这会导致极难调试的问题。

A better approach would be to place the items for your picker into an ordered container, such as NSArray. Create a special class for the picker items, for example

更好的方法是将您的选择器的物品放入有序容器中,例如NSArray. 为选择器项目创建一个特殊的类,例如

@interface MyPickerItem : NSObject
@property (readwrite, nonatomic) NSString *item1;
@property (readwrite, nonatomic) NSString *item2;
@end

create an NSArrayof such MyPickerItemobjects from your dictionary, order them the way you want them to appear in the picker view (say, alphabetically by item1, then by item2) and use NSArrayas the data source for your picker view:

从您的字典中创建一个NSArray这样的MyPickerItem对象,按照您希望它们出现在选择器视图中的方式对它们进行排序(例如,按字母顺序按item1,然后按item2)并NSArray用作选择器视图的数据源:

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
    MyPickerItem *p = myArray[row];
    switch (component) {
        case 0: return p.item1;
        case 1: return p.item2;
    }
    return @"<-ERROR->";
}

回答by Hao

This will give you randomly ordered keys

这将为您提供随机排序的密钥

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row    forComponent:(NSInteger)component{
    if (component == 0){
        return  [NSString stringWithFormat:@"%@",[myDict allKeys][row]];
    }
}

回答by trojanfoe

No, as a dictionary doesn't have order.

不,因为字典没有顺序。

You'll need to provide an array of keys, which will give you your order, in addition to the dictionary.

除了字典之外,您还需要提供一组键,这些键将为您提供订单。

@interface MyClass ()
@property NSMutableArray *keysArray;     // Don't forget to allocate this in an init method
@end

- (NSString *)pickerView:(UIPickerView *)pickerView
             titleForRow:(NSInteger)row
            forComponent:(NSInteger)component
{
    if (component == 0){
        NSString *key = self.keysArray[row];
        return self.myDict[key];    // I assume you just want to return the value
    }
    ...

回答by CrimsonChris

NSDictionaryis an unordered data structure which means that accessing with an index doesn't make much sense. You probably want an NSArrayinstead. However, if your dictionary is using NSNumbersas keys then you would access it like this.

NSDictionary是一种无序的数据结构,这意味着使用索引进行访问没有多大意义。你可能想要一个NSArray。但是,如果您的字典NSNumbers用作键,那么您将像这样访问它。

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
    if (component == 0) {
       return  [NSString stringWithFormat:@"%@", myDict[@(row)]];
    }
}