zee*_*ple 30 dictionary nested swift
我的应用程序中有一个非常复杂的数据结构,我需要操作它.我试图跟踪玩家在他们的花园中有多少种类型的错误.有十种类型的错误,每种错误有十种模式,每种模式有十种颜色.因此,可能有1000个独特的错误,我想跟踪玩家拥有的这些类型中的每一个.嵌套字典看起来像:
var colorsDict: [String : Int]
var patternsDict: [String : Any] // [String : colorsDict]
var bugsDict: [String : Any] // [String : patternsDict]
Run Code Online (Sandbox Code Playgroud)
我没有用这种语法得到任何错误或抱怨.
当我想增加玩家的bug收集时,这样做:
bugs["ladybug"]["spotted"]["red"]++
Run Code Online (Sandbox Code Playgroud)
我收到此错误: 字符串不能转换为'DictionaryIndex <String,Any>' ,错误的胡萝卜在第一个字符串下.
另一篇类似的帖子建议使用"as Any?" 在代码中,但该帖子的OP只有一个深度字典,所以可以很容易地用:dict ["string"]为Any?...
我不知道如何用多级字典做到这一点.任何帮助,将不胜感激.
Ant*_*nio 46
使用字典时,您必须记住字典中可能不存在密钥.因此,词典总是返回选项.因此,每次按键访问字典时,都必须按如下方式打开每个级别:
bugsDict["ladybug"]!["spotted"]!["red"]!++
Run Code Online (Sandbox Code Playgroud)
我认为你知道关于选项,但是要清楚,如果你100%确定字典中存在键,请使用感叹号,否则最好使用问号:
bugsDict["ladybug"]?["spotted"]?["red"]?++
Run Code Online (Sandbox Code Playgroud)
附录:这是我在游乐场测试时使用的代码:
var colorsDict = [String : Int]()
var patternsDict = [String : [String : Int]] ()
var bugsDict = [String : [String : [String : Int]]] ()
colorsDict["red"] = 1
patternsDict["spotted"] = colorsDict
bugsDict["ladybug"] = patternsDict
bugsDict["ladybug"]!["spotted"]!["red"]!++ // Prints 1
bugsDict["ladybug"]!["spotted"]!["red"]!++ // Prints 2
bugsDict["ladybug"]!["spotted"]!["red"]!++ // Prints 3
bugsDict["ladybug"]!["spotted"]!["red"]! // Prints 4
Run Code Online (Sandbox Code Playgroud)
nie*_*bot 39
另一种选择:你可以尝试打电话 dict.value( forKeyPath: "ladybug.spotted.red" )!
我的主要用例是从深层词典中读取 ad-hoc值.在我的Swift 3.1项目中,没有给出的答案对我有用,所以我去寻找并发现了Ole Begemann对Swift词典的出色扩展,并详细解释了它是如何工作的.
我已经使用我制作的Swift文件制作了一个Github的要点,我欢迎反馈.
要使用它,您可以将Keypath.swift添加到项目中,然后您可以在任何[String:Any]
字典上使用keyPath下标语法,如下所示.
考虑到你有一个像这样的JSON对象:
{
"name":"John",
"age":30,
"cars": {
"car1":"Ford",
"car2":"BMW",
"car3":"Fiat"
}
}
Run Code Online (Sandbox Code Playgroud)
存储在字典中var dict:[String:Any]
.您可以使用以下语法来获取对象的各种深度.
if let name = data[keyPath:"name"] as? String{
// name has "John"
}
if let age = data[keyPath:"age"] as? Int{
// age has 30
}
if let car1 = data[keyPath:"cars.car1"] as? String{
// car1 has "Ford"
}
Run Code Online (Sandbox Code Playgroud)
请注意,扩展程序也支持写入嵌套字典,但我还没有使用它.
我仍然没有找到一种方法来使用它来访问字典对象中的数组,但这是一个开始!我正在为Swift 寻找一个JSON指针实现,但还没找到.
小智 6
如果它只是关于检索(而不是操作),那么这里是 Swift 3 的字典扩展(准备粘贴到 Xcode 游乐场的代码):
//extension
extension Dictionary where Key: Hashable, Value: Any {
func getValue(forKeyPath components : Array<Any>) -> Any? {
var comps = components;
let key = comps.remove(at: 0)
if let k = key as? Key {
if(comps.count == 0) {
return self[k]
}
if let v = self[k] as? Dictionary<AnyHashable,Any> {
return v.getValue(forKeyPath : comps)
}
}
return nil
}
}
//read json
let json = "{\"a\":{\"b\":\"bla\"},\"val\":10}" //
if let parsed = try JSONSerialization.jsonObject(with: json.data(using: .utf8)!, options: JSONSerialization.ReadingOptions.mutableContainers) as? Dictionary<AnyHashable,Any>
{
parsed.getValue(forKeyPath: ["a","b"]) //-> "bla"
parsed.getValue(forKeyPath: ["val"]) //-> 10
}
//dictionary with different key types
let test : Dictionary<AnyHashable,Any> = ["a" : ["b" : ["c" : "bla"]], 0 : [ 1 : [ 2 : "bla"]], "four" : [ 5 : "bla"]]
test.getValue(forKeyPath: ["a","b","c"]) //-> "bla"
test.getValue(forKeyPath: ["a","b"]) //-> ["c": "bla"]
test.getValue(forKeyPath: [0,1,2]) //-> "bla"
test.getValue(forKeyPath: ["four",5]) //-> "bla"
test.getValue(forKeyPath: ["a","b","d"]) //-> nil
//dictionary with strings as keys
let test2 = ["one" : [ "two" : "three"]]
test2.getValue(forKeyPath: ["one","two"]) //-> "three"
Run Code Online (Sandbox Code Playgroud)
我有同样的问题,我想让boolValue嵌套在字典中。
{
"Level1": {
"leve2": {
"code": 0,
"boolValue": 1
}
}
}
Run Code Online (Sandbox Code Playgroud)
我尝试了很多解决方案,但是由于我缺少类型转换,因此这些方法对我没有用。因此,我使用以下代码从json获取boolValue,其中json是[String:Any]类型的嵌套字典。
let boolValue = ((json["level1"]
as? [String: Any])?["level2"]
as? [String: Any])?["boolValue"] as? Bool
Run Code Online (Sandbox Code Playgroud)
不幸的是,这些方法都不适合我,所以我构建了自己的方法来使用简单的字符串路径,例如“element0.element1.element256.element1”等。希望这可以为其他人节省时间。(只需在字符串中的元素名称之间使用点)
Json 示例:
{
"control": {
"type": "Button",
"name": "Save",
"ui": {
"scale": 0.5,
"padding": {
"top": 24,
"bottom": 32
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
第1步,将json字符串转换为字典
static func convertToDictionary(text: String) -> [String: Any]? {
if let data = text.data(using: .utf8) {
do {
return try JSONSerialization.jsonObject(with: data, options: []) as? [String: Any]
} catch {
print(error.localizedDescription)
}
}
return nil
}
Run Code Online (Sandbox Code Playgroud)
第2步,帮助程序获取嵌套对象
//path example: "control.ui.scale"
static func getDictValue(dict:[String: Any], path:String)->Any?{
let arr = path.components(separatedBy: ".")
if(arr.count == 1){
return dict[String(arr[0])]
}
else if (arr.count > 1){
let p = arr[1...arr.count-1].joined(separator: ".")
let d = dict[String(arr[0])] as? [String: Any]
if (d != nil){
return getDictValue(dict:d!, path:p)
}
}
return nil
}
Run Code Online (Sandbox Code Playgroud)
第三步,使用助手
let controlScale = getDictValue(dict:dict, path: "control.ui.scale") as! Double?
print(controlScale)
let controlName = getDictValue(dict:dict, path: "control.name") as! String?
print(controlName)
Run Code Online (Sandbox Code Playgroud)
退货
0.5
Save
Run Code Online (Sandbox Code Playgroud)