Afsara
Afsara

Reputation: 1461

Unable to call class methods in two different classes swift

i am unable to call class instance for more than one class in swift .

i have three view controllers .

1)ViewController 2)RegistrationVC 3)WebServiceInterface

WebServiceInterface having a method

func postRequest(postString: String, postUrl:String, currentVC:ViewController){
    // ... some code
}

i am able call this method from ViewController but not from RegistrationVC

from ViewController it is fine

var webServiceInterface = WebServiceInterface()
webServiceInterface.delegate = self
webServiceInterface.postRequest(str, postUrl: url, currentVC: self)

but from RegistrationVC it showing:

Cannot invoke 'postRequest' with an argument list of type '(String, postUrl: String, currentVC: RegistrationVC)'

var webServiceInterface = WebServiceInterface()
webServiceInterface.delegate = self
webServiceInterface.postRequest("asd", postUrl: "asd", currentVC: self)

enter image description here

Upvotes: 0

Views: 573

Answers (1)

nhgrif
nhgrif

Reputation: 62062

The problem is almost certainly that your RegistrationVC class is not a subclass of your ViewController class.

To resolve this, you should either change your method to expect a UIViewController (notice the UI) or to change RegistrationVC to be a subclass of ViewController, which ever makes most sense (probably the former).


We can make more sense of this by reading the error message:

Cannot invoke 'postRequest' with an argument list of type '(String, postUrl: String, currentVC: RegistrationVC)'

This error tells us a few things.

First of all, the compiler indeed is finding our method named postRequest(:postUrl:currentVC:), but the problem lies in the types of arguments we're passing.

The compiler thinks we're trying to pass a String, String, and a RegistrationVC. So how does this differ from what is actually defined for the method?

Well, we got the strings right, so it must be the last parameter that is wrong. The compiler expects a ViewController object for the third parameter, but we're trying to send a RegistrationVC (which is probably a subclass of UIViewController, which is different from ViewController (which is one of your own subclasses of UIViewController and in need of a better name).

So the best solution most likely is (depending on your implementation of postRequest(:postUrl:currentVC:) and what you're doing with currentVC: within the method) to simply change the type of that third parameter:

postRequest(postString: String, postUrl: String, currentVC: UIViewController) {
    // implementation code
}

Upvotes: 4

Related Questions