Pass a Swift class as parameter, and then call a class method out of it

John Bushnell picture John Bushnell · Apr 18, 2015 · Viewed 9.4k times · Source

I want to be able to store a class as a variable, so I can call class methods out of it later, something like this:

class SomeGenericItem: NSObject
{
    var cellClass: AnyClass

    init(cellClass: AnyClass)
    {
        self.cellClass = cellClass
    }

    func doSomething(p1: String, p2: String, p3: String)
    {
        self.cellClass.doSomething(p1, p2: p2, p3: p3)
    }
}

class SomeClass: NSObject
{
    class func doSomething(p1: String, p2: String, p3: String)
    {
        ...
    }
}

I want to be able to say something like:

let someGenericItem = SomeGenericItem(cellClass: SomeClass.self)

someGenericItem.doSomething("One", p2: "Two", p3: "Three")

What I'm trying to figure out is:

1) How would a protocol be defined so I could call class func doSomething?
2) What would the declaration of cellClass need to be?
3) What would the call look like?

Answer

Gregzo picture Gregzo · Apr 18, 2015

Protocols can't define class methods, but static methods are fine. You'll need your wrapper to be generic, and specify a 'where' constraint that guarantees the wrapped type's conformance to your protocol.

Example:

protocol FooProtocol
{
    static func bar() -> Void
}

class FooishClass : FooProtocol
{
    static func bar() -> Void
    {
        println( "FooishClass implements FooProtocol" )
    }
}

class FooTypeWrapper< T where T: FooProtocol >
{   
    init( type: T.Type )
    {
        //no need to store type: it simply is T
    }

    func doBar() -> Void
    {
        T.bar()
    }
}

Use:

let fooishTypeWrapper = FooTypeWrapper( type: FooishClass.self )
fooishTypeWrapper.doBar()