美文网首页
what is existential metatype

what is existential metatype

作者: ngugg | 来源:发表于2018-10-22 10:38 被阅读7次

    Q

    This wonders me a little bit. In Swift try the following:

    这让我感到很奇怪。 在Swift中尝试以下方法:

        override func viewDidLoad() {
            super.viewDidLoad()
            // Do any additional setup after loading the view, typically from a nib.
    
            println(self.superclass) //Outputs "(ExistentialMetatype)"
        }
    

    This gives the output on console to be ExistentialMetatype

    这使控制台上的输出为ExistentialMetatype

    Going deep into the Swift's NSObject framework it declares this superclass as a read-only computed property of type AnyClass

    深入研究Swift的NSObject框架,它将superclass声明为AnyClass类型的只读计算属性

        var superclass: AnyClass! { get }
    

    But nowhere is there a mention of this word ExistentialMetatype. I don't know why but it reminds of Objective-C's runtime (probably its the word Metatype in it). Does anyone know anything about it?

    但是没有提到这个词ExistentialMetatype。 我不知道为什么,但它提醒Objective-C的运行时(可能是它中的Metatype一词)。 有人知道吗?

    A:

    Currently, types (instances of metatypes) get printed as unhelpful things like (Metatype) or (ExistentialMetatype). AnyClass is AnyObject.Type, a metatype. So if you try to print an AnyClass you will get this unhelpful description.

    目前,类型(元类型的实例)被打印为无用的东西,如(Metatype)或(ExistentialMetatype)。 AnyClass是AnyObject.Type,一种元类型。 因此,如果您尝试打印AnyClass,您将获得这个无益的描述。

    However, classes in Objective-C are objects with nice descriptions. Swift allows casting of class types into objects so they can be used in the Objective-C way. If you cast it into AnyObject first, you will see the name of the class:

    但是,Objective-C中的类是具有良好描述的对象。 Swift允许将类类型转换为对象,因此可以以Objective-C方式使用它们。 如果先将其转换为AnyObject,您将看到该类的名称:
    println(self.superclass as AnyObject!)

    其它文章参考:
    https://goxcif.github.io/Swift-%E4%B8%AD%E7%9A%84-metatype.html

    相关文章

      网友评论

          本文标题:what is existential metatype

          本文链接:https://www.haomeiwen.com/subject/goqyzftx.html