How to call Type Methods within an instance method How to call Type Methods within an instance method ios ios

How to call Type Methods within an instance method


var staticRet:String = ClassA.staticMethod()

This works. It doesn't take any parameters so you don't need to pass in any. You can also get ClassA dynamically like this:

Swift 2

var staticRet:String = self.dynamicType.staticMethod()

Swift 3

var staticRet:String = type(of:self).staticMethod()


In Swift 3 you can use:

let me = type(of: self)me.staticMethod()


I wanted to add one more twist to this old question.

In today's Swift (I think it switched at 4, but I am not sure), we have replaced type(of: self) with Self (Note the capital "S").