简体   繁体   English

什么是Swift.AnyClass?

[英]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.

相关问题 来自AnyClass或dynamicType的数组,Swift - Array from AnyClass or dynamicType, Swift 快速调用AnyClass类函数 - Swift call AnyClass class function Swift:如何使用“ AnyClass”变量定义数组内部的类型? - Swift: how can I use an 'AnyClass' variable to define what type is inside an array? 注册TableView单元格时在Swift中使用AnyClass - Using AnyClass in Swift when registering a TableView cell Swift从AnyClass对象声明的类型化数组 - Swift Declared Typed Array from AnyClass object 如何在Swift中将UIViewController转换或转换为AnyClass - How to convert or typecast UIViewController to AnyClass in Swift 如何在Swift Generic类中使用AnyClass - How to use AnyClass in Swift Generic class 如何在Swift的AnyClass上使用performSelector()调用类方法? - How to invoke a class method using performSelector() on AnyClass in Swift? 在Swift中,Self.Type不能直接转换为ObjectiveC类扩展中的AnyClass - Self.Type cannot be directly converted to AnyClass in extension to objective-c class in swift 如何解决 Swift 警告:“常量“值”推断为“AnyClass”类型? (又名'可选<anyobject.type> '),这可能是出乎意料的”?</anyobject.type> - How to solve Swift warning: “Constant 'value' inferred to have type 'AnyClass?' (aka 'Optional<AnyObject.Type>'), which may be unexpected”?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM