Type invocation methods in instance method

Apple has a good explanation of type (class) methods here .

However, their example looks like this:

class SomeClass { class func someTypeMethod() { // type method implementation goes here } } SomeClass.typeMethod() 

I see that this same example is sown everywhere.

However, I need to call my type method from an instance of my class and which does not seem to be evaluated.

I SHOULD do something wrong, but I noticed that Apple does not yet support the class properties :( I wonder if I will be going into dry water for water.

Here is what I tried (on the playground):

 class ClassA { class func staticMethod() -> String { return "STATIC" } func dynamicMethod() -> String { return "DYNAMIC" } func callBoth() -> ( dynamicRet:String, staticRet:String ) { var dynamicRet:String = self.dynamicMethod() var staticRet:String = "" // staticRet = self.class.staticMethod() // Nope // staticRet = class.staticMethod() // No way, Jose // staticRet = ClassA.staticMethod(self) // Uh-uh // staticRet = ClassA.staticMethod(ClassA()) // Nah // staticRet = self.staticMethod() // You is lame // staticRet = .staticMethod() // You're kidding, right? // staticRet = this.staticMethod() // What, are you making this crap up? // staticRet = staticMethod() // FAIL return ( dynamicRet:dynamicRet, staticRet:staticRet ) } } let instance:ClassA = ClassA() let test:( dynamicRet:String, staticRet:String ) = instance.callBoth() 

Does anyone know me?

+47
methods types ios class swift
Jun 29 '14 at 1:25
source share
2 answers
 var staticRet:String = ClassA.staticMethod() 

It works. It does not accept any parameters, so you do not need to pass them. You can also get ClassA dynamically as follows:

Swift 2

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

Swift 3

 var staticRet:String = type(of:self).staticMethod() 
+80
Jun 29 '14 at 1:31 on
source share

In Swift 3, you can use:

 let me = type(of: self) me.staticMethod() 
+52
Sep 21 '16 at 1:56 on
source share



All Articles