获取 NSDictionary 中存在的所有键并放入数组

Get all keys exists in NSDictionary and put into Array

当我从 child "abc" 快照时,我得到了这个 NSDictionary,"3c" 可以添加更多嵌套的 childs。如何获取 NSDictionary 中的所有键并放入数组 预期结果:["abc","1a","2b","3c"]

value.allkeys 只采用相同的层次结构键

正如@AechoLiu 所写,递归方法是一种很好的实现方式。类似下面的内容:

func nestedKeys(in dictionary: [String: Any]) -> [String] {
    guard let dictionaryValues = dictionary.values.filter({ [=10=] is [String: Any] }) as? [[String: Any]] else {
        return [String]() + dictionary.keys
    }
    let subKeys = dictionaryValues.reduce([String]()) { result, dictionary in
        return result + nestedKeys(in: dictionary)
    }
    return dictionary.keys + subKeys
}

以上显然希望您的词典格式为 [String: Any]