使用字典数据迭代字典并将其添加到swift中的数组中 [英] Iterate Dictionary with dictionary data and add it to an array in swift
问题描述
我有一个包含多个字典数据的字典:
{
1455201094707 = {
};
1455201116404 = {
}:
1455201287530 = {
};
}
我必须将所有这些词典添加到swift中的数组中。
如何将字典迭代为:
for data tempDict in dataDictionary
{
self。 tempArray.addObject(tempDict)
}
错误让模式不能出现嵌套在dataDictionary的tempdict中已经不可变的
上下文
如! NSMutableDictionary
{
self.tempArray.addObject(tempDict)
}
< blockquote>
错误:参数类型元素(又名(键:AnyObject,值:AnyObject))
参数类型不符合预期类型anyobject
对于dataDictionary中的tempDict为! NSMutableDictionary
{
self.tempArray.addObject(tempDict as!AnyObject)
}
错误:无法将类型'(Swift.AnyObject,
Swift.AnyObject)'(0x1209dee18)的值转换为'Swift.AnyObject'(0x11d57d018)。
for dataDictionary中的tempDict
{
self.tempArray.addObject(tempDict) )
}
错误:类型AnyObject的值没有会员发电机
编辑
我想要最终数组为:
(
{
1455201094707 = {
};
}
{
1455201116404 = {
}:
}
)
实现这个的正确方法是什么?
任何帮助都将受到赞赏.....
我使用过代码:
var tempArray:[NSDictionary] = []
for tempDict {key,value} {
tempArray .append([key:value])
}
错误:AnyObject类型的值不符合预期的字典键类型NSCopying
代码:
让tempArray = tempDict.map({[$ 0.0:$ 0.1]})
错误:没有更多上下文的表达式类型不明确
首先,当你使用
时,让dataDictionary中的tempDict {
self .tempArray.addObject(tempDict)
}
Swift给你的元组(键,值) )在tempDict中。
所以你应该像这样迭代
sourceDict中的(键,值)
{
tempArray.append(value)
}
注意:我在这里使用原生swift结构,我的建议 - 尽可能经常使用它们(而不是ObjC)
或者你可以在字典上使用map-function。
let array = sourceDict.map({$ 0.1})
编辑。支持
(
{
1455201094707 = {
};
}
{
1455201116404 = {
}:
}
)
使用
<$ c:c> for source中的(键,值){
tempArray.append([key:value])
}
或
let array = dict.map({[$ 0.0:$ 0.1]} )
注意。如果您使用NSDictionary,则应将其强制转换为swift Dictionary
如果让dict = dict为? [String:AnyObject] {
let array = dict.map({[$ 0.0:$ 0.1]})
print(array)
}
I have a dictionary with multiple dictionary data :
{
1455201094707 = {
};
1455201116404 = {
}:
1455201287530 = {
};
}
I have to add all these dictionaries to an array in swift. How to iterate dictionary as :
for let tempDict in dataDictionary
{
self.tempArray.addObject(tempDict)
}
Error "let pattern cannot appear nested in an already immutable context"
for tempDict in dataDictionary as! NSMutableDictionary
{
self.tempArray.addObject(tempDict)
}
Error : Argument type element(aka(key:AnyObject, value:AnyObject)) argument type does not conform to expected type anyobject
for tempDict in dataDictionary as! NSMutableDictionary
{
self.tempArray.addObject(tempDict as! AnyObject)
}
Error: Could not cast value of type '(Swift.AnyObject, Swift.AnyObject)' (0x1209dee18) to 'Swift.AnyObject' (0x11d57d018).
for tempDict in dataDictionary
{
self.tempArray.addObject(tempDict)
}
Error: Value of Type AnyObject has no member generator
Edit
I want the final array as :
(
{
1455201094707 = {
};
}
{
1455201116404 = {
}:
}
)
What is the correct way to implement this?
Any help will be appreciated.....
I have used code :
var tempArray:[NSDictionary] = []
for (key, value) in tempDict {
tempArray.append([key : value])
}
Error: value of type AnyObject does not conform to expected dictionary key type NSCopying
code :
let tempArray = tempDict.map({ [$0.0 : $0.1] })
Error : type of expression is ambiguous without more context
First of all, when you use
for let tempDict in dataDictionary {
self.tempArray.addObject(tempDict)
}
Swift gives you tuple like (key, value) in tempDict.
So you should iterate like this
for (key, value) in sourceDict {
tempArray.append(value)
}
Note: I used here native swift structures, and my advise - to use them as often as possible (instead of ObjC ones)
Or you can use map-function on dictionary.
let array = sourceDict.map({ $0.1 })
Edit. For
(
{
1455201094707 = {
};
}
{
1455201116404 = {
}:
}
)
use
for (key, value) in sourceDict {
tempArray.append([key : value])
}
or
let array = dict.map({ [$0.0 : $0.1] })
Note. if you use NSDictionary you should cast it to swift Dictionary
if let dict = dict as? [String: AnyObject] {
let array = dict.map({ [$0.0 : $0.1] })
print(array)
}
这篇关于使用字典数据迭代字典并将其添加到swift中的数组中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!