I have a super-nested set of dictionaries and arrays
我有一组超嵌套的字典和数组
var lines = ["set0":["lines":["Line one","Line two"],"pickone": ["set1":"Apples","set2":"Oranges"]],"set1":["lines":["Line one","Line two"],"pickone":["set3":"Bananas","set4":"Grapefruits"]]]
var行=[“set0”:[“线”:[“一线”、“二线”),“pickone”:[set2”中的:“苹果”,“关于我校”:“橙子”]],“set2”中的:[“线”:[“一线”、“二线”),“pickone”:[“set3”:“香蕉”、“set4”:“葡萄”]]]
I have two questions:
我有两个问题:
-
Is there any way to make the structure of those nested dictionaries more readable
有没有办法让这些嵌套字典的结构更容易读
-
How can I access the values of the nested arrays
如何访问嵌套数组的值?
For instance, in a Playground I'm trying the following code:
例如,在一个操场上,我正在尝试以下代码:
lines["set0"]!["lines"]
but I'm not getting any result at all.
但是我一点结果都没有。
2 个解决方案
#1
3
As to making it readable try:
为了使它可读,试着:
var lines = [
"set0":[
"lines":[
"Line one",
"Line two"
],
"pickone":[
"set1":"Apples",
"set2":"Oranges"
]
],
"set1":[
"lines":[
"Line one",
"Line two"
],
"pickone":[
"set3":"Bananas",
"set4":"Grapefruits"
]
]
]
And as to your line:
至于你的台词:
lines["set0"]!["lines"]
I can confirm that it is working. Maybe you should give playground some time to display the result. Or, possibly, you forgot to:
我可以确认它是有效的。也许你应该给运动场一些时间来展示结果。或者,可能,你忘了:
import Foundation
#2
1
Regarding your question 2: after a little change in your code, you can traverse all of it's branches.
关于你的问题2:在你的代码稍作修改之后,你可以遍历它的所有分支。
var lines:AnyObject = ["set0":["lines":["Line one","Line two"],"pickone": ["set1":"Apples","set2":"Oranges"]],"set1":["lines":["Line one","Line two"],"pickone":["set3":"Bananas","set4":"Grapefruits"]]]
NSLog("original object:\(lines)")
NSLog("all keys array:\(lines.allKeys)")
if(lines.isKindOfClass(NSDictionary)){
for key in lines.allKeys{
var innerDictionary:AnyObject! = lines.valueForKey(key as! NSString as String)
//NSLog("inner dictionary:\(innerDictionary)")
NSLog("depth2 keys array:\(innerDictionary.allKeys)")
if(innerDictionary.isKindOfClass(NSDictionary)){
for innerKey in innerDictionary.allKeys{
var innerValue:AnyObject! = innerDictionary.valueForKey(innerKey as! NSString as String)
if(innerValue.isKindOfClass(NSDictionary)){
NSLog("Depth3 keys array:\(innerValue.allKeys)")
var mostInnerDicionary:AnyObject! = innerDictionary.valueForKey(innerKey as! NSString as String)
NSLog("most inner Dictionary:\(mostInnerDicionary)")
for mostInnerKey in mostInnerDicionary.allKeys{
var mostInnerValue:AnyObject! = mostInnerDicionary.valueForKey(mostInnerKey as! NSString as String)
NSLog("most inner Value:\(mostInnerValue)")
}
}else{
NSLog("innerValue:\(innerValue)")
}
}
}
}
}
output: run and check the output in console log
输出:运行并检查控制台日志中的输出
#1
3
As to making it readable try:
为了使它可读,试着:
var lines = [
"set0":[
"lines":[
"Line one",
"Line two"
],
"pickone":[
"set1":"Apples",
"set2":"Oranges"
]
],
"set1":[
"lines":[
"Line one",
"Line two"
],
"pickone":[
"set3":"Bananas",
"set4":"Grapefruits"
]
]
]
And as to your line:
至于你的台词:
lines["set0"]!["lines"]
I can confirm that it is working. Maybe you should give playground some time to display the result. Or, possibly, you forgot to:
我可以确认它是有效的。也许你应该给运动场一些时间来展示结果。或者,可能,你忘了:
import Foundation
#2
1
Regarding your question 2: after a little change in your code, you can traverse all of it's branches.
关于你的问题2:在你的代码稍作修改之后,你可以遍历它的所有分支。
var lines:AnyObject = ["set0":["lines":["Line one","Line two"],"pickone": ["set1":"Apples","set2":"Oranges"]],"set1":["lines":["Line one","Line two"],"pickone":["set3":"Bananas","set4":"Grapefruits"]]]
NSLog("original object:\(lines)")
NSLog("all keys array:\(lines.allKeys)")
if(lines.isKindOfClass(NSDictionary)){
for key in lines.allKeys{
var innerDictionary:AnyObject! = lines.valueForKey(key as! NSString as String)
//NSLog("inner dictionary:\(innerDictionary)")
NSLog("depth2 keys array:\(innerDictionary.allKeys)")
if(innerDictionary.isKindOfClass(NSDictionary)){
for innerKey in innerDictionary.allKeys{
var innerValue:AnyObject! = innerDictionary.valueForKey(innerKey as! NSString as String)
if(innerValue.isKindOfClass(NSDictionary)){
NSLog("Depth3 keys array:\(innerValue.allKeys)")
var mostInnerDicionary:AnyObject! = innerDictionary.valueForKey(innerKey as! NSString as String)
NSLog("most inner Dictionary:\(mostInnerDicionary)")
for mostInnerKey in mostInnerDicionary.allKeys{
var mostInnerValue:AnyObject! = mostInnerDicionary.valueForKey(mostInnerKey as! NSString as String)
NSLog("most inner Value:\(mostInnerValue)")
}
}else{
NSLog("innerValue:\(innerValue)")
}
}
}
}
}
output: run and check the output in console log
输出:运行并检查控制台日志中的输出