【问题标题】:Iterate Dictionary with dictionary data and add it to an array in swift使用字典数据迭代字典并将其快速添加到数组中
【发布时间】:2016-05-27 16:25:53
【问题描述】:

我有一个包含多个字典数据的字典:

{
    1455201094707 =     {
    };
    1455201116404 =     {
    }: 
    1455201287530 =     {
    };
}

我必须迅速将所有这些字典添加到一个数组中。 如何将字典迭代为:

for let tempDict in dataDictionary
{
     self.tempArray.addObject(tempDict)
}

错误“让模式不能出现嵌套在一个已经不可变的 上下文”

for tempDict in dataDictionary as! NSMutableDictionary
{
     self.tempArray.addObject(tempDict)
}

错误:参数类型元素(又名(键:AnyObject,值:AnyObject)) 参数类型不符合预期类型anyobject

for tempDict in dataDictionary as! NSMutableDictionary
{
     self.tempArray.addObject(tempDict as! AnyObject)
}

错误:无法转换类型 '(Swift.AnyObject, Swift.AnyObject)' (0x1209dee18) 到 'Swift.AnyObject' (0x11d57d018)。

for tempDict in dataDictionary
{
     self.tempArray.addObject(tempDict)
}

错误:AnyObject 类型的值没有成员生成器

编辑

我希望最终的数组为:

(
  {
    1455201094707 =     {
    };
  }
  {
     1455201116404 =     {
     }:
  } 
)   

实现这一点的正确方法是什么?

任何帮助将不胜感激.....

我用过代码:

var tempArray:[NSDictionary] = []

    for (key, value) in tempDict {
        tempArray.append([key : value])
    }

错误:AnyObject 类型的值不符合预期的字典键类型 NSCopying

代码:

let tempArray = tempDict.map({ [$0.0 : $0.1] }) 

错误:表达式类型不明确,没有更多上下文

【问题讨论】:

    标签: ios arrays iphone swift dictionary


    【解决方案1】:
    1. 从字典中获取所有键,因为这个NSDictionary 有属性allKeys

    2. 循环所有键,并将键的对象添加到数组中。

    3. 在上述所有步骤之前,请阅读documentation

    祝你好运!

    【讨论】:

    • 感谢您的回答。我已经编辑了这个问题,你能告诉我该怎么做吗??
    【解决方案2】:

    首先,当你使用

    for let tempDict in dataDictionary {
         self.tempArray.addObject(tempDict)
    }
    

    Swift 在 tempDict 中为您提供类似 (key, value) 的元组。

    所以你应该像这样迭代

    for (key, value) in sourceDict {
         tempArray.append(value)
    }
    

    注意:我在这里使用了原生 swift 结构,我的建议是 - 尽可能多地使用它们(而不是 ObjC 结构)

    或者您可以在字典上使用地图功能。

    let array = sourceDict.map({ $0.1 })
    

    编辑。对于

    (
      {
        1455201094707 =     {
        };
      }
      {
         1455201116404 =     {
         }:
      } 
    ) 
    

    使用

    for (key, value) in sourceDict {
         tempArray.append([key : value])
    }
    

    let array = dict.map({ [$0.0 : $0.1] })
    

    注意。如果您使用 NSDictionary,您应该将其转换为 swift Dictionary

    if let dict = dict as? [String: AnyObject] {
        let array = dict.map({ [$0.0 : $0.1] })
        print(array)
    }
    

    【讨论】:

    • 感谢您的回答。我已经编辑了这个问题,你能告诉我该怎么做吗??
    • 我使用了代码:var tempArray:[NSDictionary] = [] for (key, value) in tempDict { tempArray.append([key : value]) } 错误:anyobject 类型的值没有符合预期的字典键类型 nscopying
    • code : let array = dict.map({ [$0.0 : $0.1] }) 给出错误:表达式类型不明确,没有更多上下文
    • 再次,我使用了原生 swift 类型。您最好在任何地方使用它们或投射到它们。
    • 感谢@Anton,它显示了一个问题“从 NSMutableDictionary 转换为不相关类型 [string:anyobject] 总是失败”,但输出是正确的。还有一件事,如何将这个数组添加到 NSmutableArray 中,以在 tableView 中显示数据?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-01-15
    • 1970-01-01
    • 2015-03-24
    • 2021-10-29
    • 2020-02-19
    • 1970-01-01
    • 2019-09-21
    相关资源
    最近更新 更多