Created
April 1, 2017 03:34
-
-
Save wh1pch81n/76932533a4a5813f6f2272fb8d3ce79b to your computer and use it in GitHub Desktop.
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
// A possible technique for testing a json transfers to a struct properly | |
let json: [String: Any] = [ | |
"alpha" : "aaaa", | |
"beta" : 98, | |
"delta" : 23.7 | |
] | |
// the keys that we care about | |
enum ABCEnum: String { | |
case alpha | |
case beta | |
case delta | |
} | |
// The struc that will hold and be initialized by the json | |
struct ABC { | |
var alpha: String = "" | |
var beta: Int = 6 | |
var delta: Double = 0.0 | |
init(dict: Any?) { | |
for (k, v) in (dict as? [String: Any]) ?? [:] { | |
switch ABCEnum(rawValue: k) { | |
case .alpha?: alpha = (v as? String) ?? "" | |
case .beta?: beta = (v as? Int) ?? 0 | |
case .delta?: delta = (v as? Double) ?? 0.0 | |
case .none: | |
() | |
} | |
} | |
} | |
func funTimes() { | |
} | |
var m: Int = 9 | |
static var ss: Int = 9 | |
} | |
// Use a mirrow technique to go over every property of the struct. This will help ensure that all new properties added are tested | |
let a = ABC(dict: json) | |
let m = Mirror(reflecting: a) | |
for (_k, v) in m.children where _k != nil { | |
let k = _k! | |
switch ABCEnum(rawValue: k) { | |
case .alpha?: print(a.alpha == "aaaa") | |
case .beta?: print(a.beta == 98) | |
case .delta?: print(a.delta == 23.7) | |
case .none: | |
print("unhandled property: \(k)") | |
} | |
} | |
print("done") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment