[英]What is Swift.AnyClass?
I was looking into UITableView
class and I found: 我正在研究UITableView
类,我发现:
open func register(_ cellClass: Swift.AnyClass?, forCellReuseIdentifier identifier: String)
Reading here I know AnyClass
is actualy a typealias. 在这里阅读我知道AnyClass
是一个类型。
typealias AnyClass = AnyObject.Type
What I don't understand is what is: Swift.AnyClass
What does the Swift
do? 我不明白的是: Swift.AnyClass
Swift
做什么?
I already know that the parameter it takes is something like CustomTableViewCell.self
我已经知道它所采用的参数类似于CustomTableViewCell.self
Just to clear this out, as someone already said, Swift
is the name of the virtual module for the runtime, every object, type and builtin defined in the language will also be accessible from this namespace. 为了清除这一点,正如有人已经说过的, Swift
是运行时的虚拟模块的名称,该语言中定义的每个对象,类型和内置也可以从该命名空间访问。
In this case, why the type is declared as Swift.AnyClass
instead of simply AnyClass
? 在这种情况下,为什么类型被声明为Swift.AnyClass
而不是简单的AnyClass
?
I don't think that it was required to avoid name-clashing with some type defined in UITableView scope this time. 我不认为这次要求避免与UITableView范围中定义的某些类型发生名称冲突。 This could be related to the fact that UIKit is still written in Objective-C. 这可能与UIKit仍然用Objective-C编写的事实有关。
They likely implemented a "workaround" to use the better-looking AnyClass
type in that function prototype, that being defined in the Swift runtime would not normally be available in Objective-C as a global typedef. 他们可能实现了一个“解决方法”,在该函数原型中使用外观更好的AnyClass
类型,在Swift运行时中定义的内容通常不会在Objective-C中作为全局typedef使用。
My guess is that they manually imported the Swift module (or a smaller mockup module with just a few definitions) in UIKit and what you see there is the result of that. 我的猜测是他们在UIKit中手动导入Swift模块(或者只有一些定义的较小的模型模块),你看到的就是结果。
Some more information to add on to the accepted answer: 更多信息可以添加到已接受的答案中:
First of all Swift.AnyClass
is type alias for AnyObject.Type
. 首先, Swift.AnyClass
是类型别名AnyObject.Type
。 That means that it's a metatype which can hold any 'classtype', therefore the name AnyClass
in first place. 这意味着它是一个可以保存任何'classtype'的元类型,因此首先命名为AnyClass
。
class A {}
class B {}
var anyClass: AnyClass? = nil /* could be ': AnyObject.Type' */
anyClass = A.self
anyClass = B.self
var anyInstanceOfAClass: AnyObject? = nil
anyInstanceOfAClass = A()
anyInstanceOfAClass = B()
Swift
is the actual module name for the standard library. Swift
是标准库的实际模块名称。 You can use it yourself if you need a way to disambiguate things. 如果您需要一种消除歧义的方法,您可以自己使用它。 For Instance you can do something like this: 对于Instance,您可以执行以下操作:
struct Array<T> {
private var array: Swift.Array<T> { ... }
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.