Created
July 11, 2020 02:20
-
-
Save liuzhida33/c59ff2df0241a6cab7fd691b75bb3587 to your computer and use it in GitHub Desktop.
如何在Swift中轻松解析深度json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public func resolve<T>(_ jsonDictionary: [String: Any], keyPath: String) -> T? { | |
var current: Any? = jsonDictionary | |
keyPath.split(separator: ".").forEach { component in | |
if let maybeInt = Int(component), let array = current as? Array<Any> { | |
current = array[maybeInt] | |
} else if let dictionary = current as? JSONDictionary { | |
current = dictionary[String(component)] | |
} | |
} | |
return current as? T | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment