Apple 是否有任何理由决定使用 AnyClass 而不是使用 UITableViewCell.Type?

Is there any reason Apple decided to use AnyClass rather than using UITableViewCell.Type?

使用下面的代码,您可以为 cellClass

传入 UIViewController.self
func register(_ cellClass: AnyClass?, forCellReuseIdentifier identifier: String)

和运行进入错误。

但是如果写成:

func register(_ cellClass: UITableViewCell.Type?, forCellReuseIdentifier identifier: String)

任何通过的都必须是 UITableViewCell 的子类。它将不再接受 UIViewController.self。如果通过它会在编译时抛出错误,这样更好。

那么为什么不这样写呢?

我不是在征求意见,只是想知道如果 Apple 按照我建议的方式编写是否有任何缺点,或者是否存在我不知道的任何语言限制?

UITableView 的 API 来自 Objective-C 头文件。 Objective-C 中的 cellClass 参数只是 Class,Swift 等价于 AnyClass。与 Swift 不同,Objective-C 不支持声明 Class (AnyClass) 的值是任何特定类型。

因此,当此 API 翻译为 Swift 时,您将在 Swift 中留下通用 AnyClass

奇怪的是,文档没有指定传入的 class/type 应该是 UITableViewCell(或子类)。

Objective-C 中的此限制在此处讨论(感谢 Larme):Specific Class type parameter in Objective-C