4

I was looking into UITableView class and I found:

open func register(_ cellClass: Swift.AnyClass?, forCellReuseIdentifier identifier: String)

Reading here I know AnyClass is actualy a typealias.

typealias AnyClass = AnyObject.Type

What I don't understand is what is: Swift.AnyClass What does the Swift do?

I already know that the parameter it takes is something like CustomTableViewCell.self

uraimo
  • 19,081
  • 8
  • 48
  • 55
mfaani
  • 33,269
  • 19
  • 164
  • 293
  • I'm gonna take a stab that it's a superclass to all other classes. Something of type `AnyClass` can be of any type. The `Swift` part is probably a namespace for language related stuff. – Carcigenicate Jan 17 '17 at 22:36
  • @Carcigenicate *The Swift part is probably a namespace for language related stuff* <--Contrast as into what? Contrast to what other syntax? – mfaani Jan 17 '17 at 22:40
  • Contrasted to often used classes/aliases. They probably figured most people won't be using `AnyType` very often, so it wasn't worth including it in the global namespace. – Carcigenicate Jan 17 '17 at 22:44
  • 1
    See [Swift's standard library and name collision](http://stackoverflow.com/q/25231650/2976878) – the `Swift.` prefix means you're referring to something in the `Swift` module. This isn't explicitly needed though (unless in order to disambiguate) as Swift has implicit top-level namespacing. – Hamish Jan 17 '17 at 23:03
  • @Hamish Thanks...So does this mean there was a namespace conflict somewhere and Apple had to use this? Also see [how to use namespaces in Swift](http://stackoverflow.com/questions/24002821/how-to-use-namespaces-in-swift/24293236#24293236) – mfaani Jan 17 '17 at 23:09
  • @Honey It's most likely just to be how the Swift header for `UITableView` is auto-generated. – Hamish Jan 17 '17 at 23:35

2 Answers2

4

Some more information to add on to the accepted answer:

First of all Swift.AnyClass is type alias for AnyObject.Type. That means that it's a metatype which can hold any 'classtype', therefore the name AnyClass in first place.

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. You can use it yourself if you need a way to disambiguate things. For Instance you can do something like this:

struct Array<T> {
   private var array: Swift.Array<T> { ... } 
}
mfaani
  • 33,269
  • 19
  • 164
  • 293
DevAndArtist
  • 4,971
  • 1
  • 23
  • 48
2

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.

In this case, why the type is declared as Swift.AnyClass instead of simply AnyClass?

I don't think that it was required to avoid name-clashing with some type defined in UITableView scope this time. This could be related to the fact that UIKit is still written in 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.

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.

uraimo
  • 19,081
  • 8
  • 48
  • 55