Rembrandt Q. Einstein Rembrandt Q. Einstein - 1 year ago 73
Swift Question

Why is Bool "AnyObject" instead of "Any"?

I have a simple question: Why does

qualify as

According to Apple's documentation:

So why does this statement pass?

let bool = true
let explicitBool: Bool = true

if (bool is AnyObject){
print("I'm an object")

if (explicitBool is AnyObject){
print("I'm still an object!")

Answer Source

Because it's being bridged to an NSNumber instance.

Swift automatically bridges certain native number types, such as Int and Float, to NSNumber. - Using Swift with Cocoa and Objective-C (Swift 2.2) - Numbers

Try this:

let test = bool as AnyObject
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download