【问题标题】:how to convert array which is a dictionary objective-c如何转换作为字典objective-c的数组
【发布时间】:2017-04-07 12:19:53
【问题描述】:

我的数组

{
    cash = 860;
    flat = 157;
    propertyName = "Four seasons";
},
    {
    cash = 540;
    flat = 156;
    propertyName = "Four seasons";
},
    {
    cash = 560;
    flat = 155;
    propertyName = "Four seasons";
}

如何用字典将数组是一个字典,在另一个数组中具有相同的字符串

{
    cash1 = 860;
    cash2 = 540;
    cash3 = 560;
    flat1 = 157;
    flat2 = 156;
    flat3 = 155;
    propertyName = "Four seasons";
}, ...

【问题讨论】:

  • 你尝试了什么?
  • 数组中的所有字典都有相同的键?
  • 是的,有 4 个键
  • NSMutableDictionary *finalDict = [[NSMutableDictionary alloc] init]; for (NSUInteger i = 0; i < [arrayOfDict count]; i ++){NSDictionary *subDict = arrayOfDict[i]; for (NSString *aKey in subDict){[finalDict setObject:subDict[aKey] forKey:[NSString stringWithFormat:@"%@%ld", aKey, i+1]];}}?

标签: objective-c nsstring nsmutablearray nsdictionary


【解决方案1】:

这可能会对你有所帮助。

仅供参考,这要简单得多...

   NSMutableDictionary *result = [[NSMutableDictionary alloc]init];
   for (NSDictionary *dict in yourArray)
 {
    [result addEntriesFromDictionary:dict];
 }

【讨论】:

  • 如果所有字典都有相同的键,那么您将只有最后一个字典。因为字典中的键是唯一性的。
猜你喜欢
  • 2011-09-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-05-26
  • 2017-01-06
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多