How to call Swift instance method from type method?

Within a custom class I have an instance method that I would like to call from a type (class) method. How to do that?

class MyClass {

let kSomeValue = "my value"

private func instanceMethod() -> String {
    return kSomeValue
}

class func typeMethod() {
    let someValue = instanceMethod()
    // ...
}

Calling instanceMethod() from within the type method does not work, the compiler wants to have some class reference or says: “Cannot invoke instanceMethod() with no arguments”. Tried instanceMethod(self) with no success either…

I can make instanceMethod() a class function, but then I still have the problem for kSomeValue. And yes, I realize this example is not good software design, but it is the simplest representation for the issue I am having.


Source: oop

Leave a Reply