【问题标题】:iOS how can I dump multiple properties of an object into a dictionary using a predicate or KVC?iOS 如何使用谓词或 KVC 将对象的多个属性转储到字典中?
【发布时间】:2013-12-25 03:50:00
【问题描述】:

我有一个类的属性变得臃肿,现在大约有 30 个,其中大部分是整数枚举类型。

我的代码目前在很多地方都使用了这个,我正在尝试慢慢地转向新的字典表示。

我希望从这个对象中创建一个字典,但只包含非 0 的值(具有一些数据的值)。

是否有一些objective-c键值编码魔法可以帮助我简化这种方法的编写?

@property(nonatomic)kGrade grade;
@property(nonatomic)kQuality quality;
//a whole bunch more properties

    -(NSMutableDictionary*)itemAsDictionary
    {
        if(itemDictionary !=nil)
        {
            return itemDictionary;
        }else
        {
            itemDictionary = [[NSMutableDictionary alloc] initWithCapacity:40];


            //I really dont want to write a whole bunch of such statements
            if(self.grade>0)
            {
                [itemDictionary setObject:@(self.grade) forKey:@"grade"];
            }
            //add 39 other items

        }
        return itemDictionary;
    }

【问题讨论】:

    标签: ios objective-c nsdictionary nsobject key-value-coding


    【解决方案1】:

    您可以使用dictionaryWithValuesForKeys:,然后删除任何等于零的条目:

    - (NSMutableDictionary *)itemAsDictionary {
        NSArray *keyArray = @[@"grade",
                              @"otherProperty"
                              // etc.
                              ];
    
        NSMutableDictionary *itemDictionary = [[self dictionaryWithValuesForKeys:keyArray] mutableCopy];
    
        NSArray *keys = [itemDictionary allKeys];
    
        for(NSString *key in keys) {
            NSNumber *item = itemDictionary[key];
            if(item.doubleValue == 0.0)
                [itemDictionary removeObjectForKey:key];
        }
    
        return itemDictionary;
    }
    

    【讨论】:

      【解决方案2】:
      - (NSMutableDictionary *)itemAsDictionary
      {
          NSMutableDictionary *resultDic = [[NSMutableDictionary alloc] init];
          unsigned int outCount, i;
          //type the name of the class you want to turn to dictionary
          objc_property_t *properties = class_copyPropertyList([Item class], &outCount);
          for (i = 0; i < outCount; i++) {
              objc_property_t property = properties[i];
              const char *propName = property_getName(property);
              if (propName) {
                  NSString *propertyName = [NSString stringWithUTF8String:propName];
                  id propertyValue = [self valueForKey:propertyName];
      
                  //set up the filter here
                  if([propertyValue isKindOfClass:[NSNumber class]] && [propertyValue intValue] > 0)
                  {
                      [resultDic setObject:propertyValue forKey:propertyName];
                  }else if(![propertyValue isKindOfClass:[NSNumber class]] && propertyValue !=nil)
                  {
                      //copy all non-nil variables
                      [resultDic setObject:propertyValue forKey:propertyName];
                  }
      
              }
          }
          free(properties);
          return resultDic;
      }
      

      我尝试从一个类中获取属性列表,然后返回一个字典,它成功了。也许你可以试试。

      编辑:如果我在子类中使用 [self class],上面的代码不起作用,但是当我将 self 替换为 [Item class] 时,它确实起作用。

      我添加了一些简单的检查 - 我对大于 0 的数字和非 nil 属性感兴趣,例如字符串和图像。请注意,如果我删除第二个数字检查,代码将在结果字典中添加零。

      【讨论】:

      • 如何使用 chid 字典找到具有详细对象的对象?你能告诉我吗
      猜你喜欢
      • 2016-03-04
      • 2011-11-22
      • 2013-11-09
      • 2011-05-20
      • 2016-02-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-08
      相关资源
      最近更新 更多