【问题标题】:Getting NSDictionary key by index?按索引获取 NSDictionary 键?
【发布时间】:2014-05-14 14:54:37
【问题描述】:

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

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

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

编辑--

myDict 类似于:

项目 1

  • 子项 1
  • 子项 2

第 2 项

  • 子项 1
  • 子项 2

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

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

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

【问题讨论】:

  • 按索引获取字典键是什么意思?通过显示示例或更多代码来详细说明如何?
  • 字典没有索引,可以使用 allKeys 方法获取键的数组。
  • 阅读 NSDictionary 规范,看看有哪些功能可用。

标签: ios objective-c ios7 nsdictionary uipickerview


【解决方案1】:

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

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

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

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

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

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

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

- (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->";
}

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

      【解决方案3】:

      不,因为字典没有顺序。

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

      @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
          }
          ...
      

      【讨论】:

      • +1 用于提醒分配NSMutableDictionary。有多少次我因为忘记这样做而把自己逼疯了……
      【解决方案4】:

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

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-03-16
        • 1970-01-01
        相关资源
        最近更新 更多