Is Float, Double, Int an AnyObject?

前端 未结 3 1836
执念已碎
执念已碎 2021-01-07 19:49

I read inline documentation of Swift and I am bit confused.

1) Any is a protocol that all types implicitly conform.

2) AnyObject

3条回答
  •  暖寄归人
    2021-01-07 20:43

    class Test {
    
    static func test() {
        let anyObjectsValues: [AnyObject] = [1, "Two", 3, "Four"] as [AnyObject]
        anyObjectsValues.forEach { (value) in
            switch value {
            case is Int:
                print("\(value) is an Int!")
            case is String:
                print("\(value) is a String!")
            default:
                print("\(value) is some other type!")
            }
        }
    }
    

    }

    I have not imported UIKit or Foundation frameworks. Why compiler is not giving any error? Even it printing the result.

    Output:

    1 is an Int!

    Two is a String!

    3 is an Int!

    Four is a String!

    Does anybody have an idea?

提交回复
热议问题