ios - How do I call a static function of a class by its instance -
i have simple class:
class tableitem { class func celliden() -> string { return "tableitem" } }
and subclass of tableitem
class editableitem { override class func celliden() -> string { return "editableitem" } }
then, somewhere in code, have this:
var items: [tableitem] = [tableitem(), editableitem()]
now, want is, iterate through items , call each tableitem
's static celliden()
function.
for in items { var iden = i.self.celliden() }
however, tells me tableitem not have member called 'celliden'
how can call static method of class instance?
note: can't call tableitem.celliden()
because i
can tableitem
or editableitem
you need runtime type of i
. every instance has dynamictype
property returns runtime (dynamic) type:
var iden = i.dynamictype.celliden()
it's documented in the swift programming language: “dynamic type expression”.
Comments
Post a Comment