Last active
September 7, 2016 15:09
-
-
Save bolismauro/5f903c0e6e61d768b24bc383ae9a26dc to your computer and use it in GitHub Desktop.
Super simple json wrapper
This file contains 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
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