Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/116.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 为嵌套的NSDictionary';生成键值编码路径的完整列表;s_Ios_Objective C_Nsdictionary_Key Value Coding - Fatal编程技术网

Ios 为嵌套的NSDictionary';生成键值编码路径的完整列表;s

Ios 为嵌套的NSDictionary';生成键值编码路径的完整列表;s,ios,objective-c,nsdictionary,key-value-coding,Ios,Objective C,Nsdictionary,Key Value Coding,我有一个包含键和值的NSDictionary,一些值也将是NSDictionarys。。。达到任意(但合理)的水平 我想获得所有有效KVC路径的列表,例如: { "foo" = "bar", "qux" = { "taco" = "delicious", "burrito" = "also delicious", } } 我会得到: [ "foo", "qux", "qux.taco", "qux.burr

我有一个包含键和值的
NSDictionary
,一些值也将是
NSDictionary
s。。。达到任意(但合理)的水平

我想获得所有有效KVC路径的列表,例如:

{
    "foo" = "bar",
    "qux" = {
        "taco" = "delicious",
        "burrito" = "also delicious",
    }
}
我会得到:

[
    "foo",
    "qux",
    "qux.taco",
    "qux.burrito"
]

有没有一种简单的方法可以做到这一点?您可以通过
所有键递归。显然,键是键路径,如果值是NSDictionary,则可以递归和追加

- (void) obtainKeyPaths:(id)val intoArray:(NSMutableArray*)arr withString:(NSString*)s {
    if ([val isKindOfClass:[NSDictionary class]]) {
        for (id aKey in [val allKeys]) {
            NSString* path = 
                (!s ? aKey : [NSString stringWithFormat:@"%@.%@", s, aKey]);
            [arr addObject: path];
            [self obtainKeyPaths: [val objectForKey:aKey] 
                       intoArray: arr 
                      withString: path];
        }
    }
}
以下是如何称呼它:

NSMutableArray* arr = [NSMutableArray array];
[self obtainKeyPaths:d intoArray:arr withString:nil];

之后,
arr
包含您的关键路径列表。

这是我在记下Matt的答案后写的一个快速版本

extension NSDictionary {
    func allKeyPaths() -> Set<String> {
        //Container for keypaths
        var keyPaths = Set<String>()
        //Recursive function
        func allKeyPaths(forDictionary dict: NSDictionary, previousKeyPath path: String?) {
            //Loop through the dictionary keys
            for key in dict.allKeys {
                //Define the new keyPath
                guard let key = key as? String else { continue }
                let keyPath = path != nil ? "\(path!).\(key)" : key
                //Recurse if the value for the key is another dictionary
                if let nextDict = dict[key] as? NSDictionary {
                    allKeyPaths(forDictionary: nextDict, previousKeyPath: keyPath)
                    continue
                }
                //End the recursion and append the keyPath
                keyPaths.insert(keyPath)
            }
        }
        allKeyPaths(forDictionary: self, previousKeyPath: nil)
        return keyPaths
    }
}
扩展字典{ func allkeypath()->Set{ //关键路径容器 var keypath=Set() //递归函数 func allkeypath(用于字典dict:NSDictionary,以前的keypath:String?){ //循环浏览字典键 用于输入dict.allKeys{ //定义新的关键路径 guard let key=key as?字符串else{continue} 让keyPath=path!=nil?\(path!)。\(key):key //如果键的值是另一个字典,则递归 如果让nextDict=dict[key]作为NSDictionary{ 所有关键路径(对于字典:nextict,前一个关键路径:关键路径) 持续 } //结束递归并附加keyPath 键路径。插入(键路径) } } 所有关键路径(对于字典:self,以前的关键路径:nil) 返回键路径 } }
次要注释:
用于(val中的id aKey)
也会起作用,因为字典的快速枚举会枚举它的键;因此,我不必说“通过
所有键递归”
;我本可以说“通过键递归”。这基本上就是我所做的,但我是非递归地做的总是重复!否则生活就是一个无聊的地方。非常讽刺的是,在一个叫做堆栈溢出的地方。