-1

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")
    }

}

Pætur Magnussen
  • 901
  • 1
  • 11
  • 24
  • 1
    You need to call an instance method on an actual instance. – NobodyNada Nov 19 '14 at 22:43
  • 2
    You can't call an instance method from a class method because you don't have an instance of the class. The "missing" parameter" to `someMethod` is an implied `self` that is passed for you. – vacawama Nov 19 '14 at 22:43

1 Answers1

2

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() { ...
    
Antonio
  • 71,651
  • 11
  • 148
  • 165
  • Thanks, but I want to call "updateData()" from another class which is why it was a class method. Too bad it's not possible. – Pætur Magnussen Nov 19 '14 at 22:48
  • I wouldn't say that it's not possible - it's just impossible. An instance method requires an instance, but you want it to work with no instance. – Antonio Nov 19 '14 at 22:51
  • 1
    You can call instance methods from other classes, you just have to supply the instance pointer to the other class. You probably could benifit from sone studying of object oriented programming and classes. – zaph Nov 19 '14 at 22:53