Generate a complete list of key-value coding paths for nested NSDictionary's?

可紊 提交于 2019-12-07 13:04:54

问题


I have an NSDictionary that contains keys and values, and some values will also be NSDictionarys... to an arbitrary (but reasonable) level.

I would like to get a list of all valid KVC paths, e.g. given:

{
    "foo" = "bar",
    "qux" = {
        "taco" = "delicious",
        "burrito" = "also delicious",
    }
}

I would get:

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

Is there a simple way to do this that already exists?


回答1:


You could recurse through allKeys. A key is a key path, obviously, and then if the value is an NSDictionary you can recurse and append.

- (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];
        }
    }
}

And here is how to call it:

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

Afterwards, arr contains your list of key paths.




回答2:


Here is a Swift version I wrote after taking note from Matt's answer.

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
    }
}


来源:https://stackoverflow.com/questions/16492158/generate-a-complete-list-of-key-value-coding-paths-for-nested-nsdictionarys

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!