Doesn't work to call instance method from class method

226 views Asked by At

I'm trying to call an instance method from a class method in Swift, but I keep getting the error "Missing argument for parameter #1 in call" on the "someMethod()" call.

Do you know why?

Here's the code:

class ViewController: UIViewController {

    class func updateData() {
        someMethod()
    }

    func someMethod() {
       NSLog("someMethod")
    }

}

1

There are 1 answers

3
Antonio On

updateData is declared as a class method (i.e. static), and it's executed in the context of the class type and not a class instance. On the other hand, someMethod is an instance method.

You cannot execute an instance method from a static method, unless you provide an instance.

Without knowing the logic of your app, it's hard to figure out how the problem should be solved. Some possible solutions:

  1. make uploadData an instance method, by removing class from its signature:

    func updateData() { ...
    
  2. make someMethod a static method:

    class func someMethod() { ...