Besides of Any and AnyObject, there is another AnyClass which could express the concept of "any" in Swift. Under the hood, AnyClass is no more than a typealias:

typealias AnyClass = AnyObject.Type

By accessing with AnyObject.Type, we can get a meta type of a Swift type. This will be used when we declare this meta type of a specified type. For example, A.Type means the meta type of type A. When we need to get an instance of A.Type, we can use .self on A:

class A {

}

let typeA: A.Type = A.self

In Swift, .self could be used on a type to extract its meta type or on an instance of a type. The latter use will return the instance itself, so it does not make any sense, while the former one is useful in some cases. We will see several of them soon.

Now with these basic information, we know AnyObject.Type, or AnyClass - a more common form of it - is nothing special than a type of AnyObject. Since A is a class, its instances could be expressed with AnyObject, we can specify the meta type of A is AnyClass without force conversion:

class A {

}

let typeA: AnyClass = A.self

Similarly, if there is a class method in A, we can call it with typeA as well:

class A {
    class func method() {
        print("Hello")
    }
}

let typeA: A.Type = A.self
typeA.method()

// Or
let anyClass: AnyClass = A.self
(anyClass as A.Type).method()

You may wonder if it means less here, since we could just call A.method(). Truly, for a single type, we do not have much reason to care about its meta. However, we have to admit the power of meta type or meta programming, which could allow us to write great code. Let's see an example below:

class MusicViewController: UIViewController {

}

class AlbumViewController: UIViewController {

}

let usingVCTypes: [AnyClass] = [MusicViewController.self,
                                AlbumViewController.self]

func setupViewControllers(vcTypes: [AnyClass]) {
    for vcType in vcTypes {
        if vcType is UIViewController.Type {
            let vc = (vcType as UIViewController.Type).`new`()
            print(vc)
        }

    }
}

setupViewControllers(usingVCTypes)

Although we are using code to declare MusicViewController and AlbumViewController, there is no barrier for us to read these types from a config file or somewhere else. Then we pass meta types and create view controllers. setupViewControllers does not care the input type and will create instance for input. It provides a way for us to set up the frame first and then config view controllers by something like DSL, without retouching the Swift code anymore.

In Cocoa API, it is not rare we can meet a method that needs AnyClass as parameter. In this case, we will use .self to get the meta type and pass it to the API. A most common case would be registered a cell for tableView:

self.tableView.registerClass(
    UITableViewCell.self, forCellReuseIdentifier: "myCell")

.Type is a meta type of type. In Swift, there are 3 kinds of types: class, struct and enum. Where is protocol?

Yes, we can define protocol, but it is not a real type in Swift. If we wish to obtain the meta information of a protocol, we can use .Protocol instead, which is similar to .Type.