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

故事扮演 提交于 2020-04-12 15:59:15

问题


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?


回答1:


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()


来源:https://stackoverflow.com/questions/29712667/pass-a-swift-class-as-parameter-and-then-call-a-class-method-out-of-it

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!