Created
March 27, 2018 09:48
-
-
Save alextud/bfbf7f73efabb976e206b8a87805fdd9 to your computer and use it in GitHub Desktop.
JSONSerialization how to check for bool type
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
import UIKit | |
import Foundation | |
let string = "{\"bool\" : true, \"int\": 1 }" | |
let json = try! JSONSerialization.jsonObject(with: string.data(using: .utf8)!, options: []) | |
if let dict = json as? [String: Any] { | |
dict["bool"] is NSNumber | |
dict["int"] is NSNumber | |
dict["bool"] is Bool | |
dict["int"] is Bool | |
type(of: dict["bool"]! ) | |
type(of: dict["int"]! ) | |
String(describing: type(of: dict["bool"]!) ) | |
String(describing: type(of: dict["int"]!) ) | |
String(describing: dict["bool"]! ) | |
String(describing: dict["int"]! ) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment