Skip to content

Instantly share code, notes, and snippets.

@alextud
Created March 27, 2018 09:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alextud/bfbf7f73efabb976e206b8a87805fdd9 to your computer and use it in GitHub Desktop.
Save alextud/bfbf7f73efabb976e206b8a87805fdd9 to your computer and use it in GitHub Desktop.
JSONSerialization how to check for bool type
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