Skip to content

Instantly share code, notes, and snippets.

@bolismauro
Last active September 7, 2016 15:09
Show Gist options
  • Save bolismauro/5f903c0e6e61d768b24bc383ae9a26dc to your computer and use it in GitHub Desktop.
Save bolismauro/5f903c0e6e61d768b24bc383ae9a26dc to your computer and use it in GitHub Desktop.
Super simple json wrapper
enum JSONResult<V> {
case value(V)
case none
func get(_ key: String) -> JSONResult<[String: Any]> {
if let v = self.get(key, as: [String: Any].self) {
return .value(v)
}
return .none
}
func get<T>(_ key: String, as: T.Type) -> T? {
switch self {
case let .value(v):
if let v = v as? [String: Any] {
if let nestedValue = v[key] {
return nestedValue as? T
}
}
return nil
case .none:
return nil
}
}
var string: String? {
switch self {
case let .value(v):
return v as? String
default:
return nil
}
}
}
//let a = JSONResult.value(dictionary).get("e", as: Int.self)//.get("aa").get("b").get("c", as: String.self)
//print(a)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment