Well, that is how
JSONSerialization
works
Right. Due to its Objective-C heritage, it can’t return JSON Boolean values as Swift Bool
values because those are value types, not objects.
What you’ll find, however, is that it does use CFBoolean
for JSON Booleans, so you can detect whether the original was a Boolean or a number by testing against kCFBooleanTrue
and kCFBooleanFalse
. Consider:
// One of these 1’s is not like other…
let m = dictionary!["meta"]! as! [String:Any]
let d = m["demo"]! as! NSNumber
print(d) // 1
print(d === kCFBooleanTrue) // true
print(d === kCFBooleanFalse) // false
let e = dictionary!["employeeID"]! as! NSNumber
print(e) // 1
print(e === kCFBooleanTrue) // false
print(e === kCFBooleanFalse) // false
Share and Enjoy
Quinn “The Eskimo!” @ DTS @ Apple