为什么Bool"AnyObject"而不是"Any"?

Rem*_*ein 9 swift swift-playground swift2

我有一个简单的问题:为什么Bool符合AnyObject Apple的文档资格:

那么为什么这个陈述会通过呢?

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!")
}
Run Code Online (Sandbox Code Playgroud)

Ale*_*ica 8

因为它被桥接到NSNumber实例.

Swift自动将某些本机数字类型(例如Int和Float)连接到NSNumber. - 使用Swift与Cocoa和Objective-C(Swift 2.2) - 数字

试试这个:

let test = bool as AnyObject
print(String(test.dynamicType))
Run Code Online (Sandbox Code Playgroud)

  • 我认为基金会必须进口才能工作? (2认同)