Bridging a Swift.Bool to AnyObject looses Type information

Originator:alex.sporn
Number:rdar://27922204 Date Originated:19-Aug-2016
Status:Open Resolved:
Product:Developer Tools Product Version:Version 8.0 beta 6 (8S201h)
Classification:Serious Bug Reproducible:Always
 
Summary:
Using the latest Xcode8 beta6 SDK when bridging a Swift.Bool to AnyObject or to NSNumber (e.g. to be serialized by JSONSerialization), the Type information is lost and the Bool is serialized as an integer (0 or 1) instead of as a string ("true" or "false")

let bool = true                              // Bool.Type
let boolAsAnyObject = bool as AnyObject      // is bridged to _SwiftTypePreservingNSNumber.Type
let boolAsNSNumber = bool as NSNumber        // is bridged to _SwiftTypePreservingNSNumber.Type
let trueAsAnyObject = true as AnyObject      // is also bridged to _SwiftTypePreservingNSNumber.Type
let trueAsNSNumber = true as NSNumber        // is bridged to __NSCFBoolean.Type
let nsNumberFromBool = NSNumber(value: bool) // __NSCFBoolean.Type

Only __NSCFBoolean.Type seems to be correctly serialized by JSONSerialization as a string with "true" or "false"

See the attached Playground.

Steps to Reproduce:
Open attached Playground that shows the issue.

Expected Results:
JSONSerialization serializes Bool as string with "true" or "false"

Actual Results:
JSONSerialization serializes Bool as integer with 1 or 0

Version:
Version 8.0 beta 6 (8S201h)

Notes:


Configuration:


Attachments:
'BoolAsAnyObject.playground.zip' was successfully uploaded.

Comments

Might be directly connected to https://bugs.swift.org/browse/SR-2381

By alex.sporn at Aug. 19, 2016, 4:33 p.m. (reply...)

Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!