A method without parameters is calling for an argument

后端 未结 2 927
野的像风
野的像风 2021-01-27 15:13

I have a class named Location that has several methods in it that do not have any parameters.

However, when I try to create a variable with the result of th

相关标签:
2条回答
  • 2021-01-27 15:45

    Because you're trying to call it as a class function. You should be creating an instance of Location and calling the function on that. Note also that it returns String Where your code is telling the compiler you're expecting it to return a Location.

    0 讨论(0)
  • 2021-01-27 15:58

    These methods are not class methods, they are instance methods. You must call them on an instance of the Location class, not on the class itself. Evidently, Swift can call instance methods similarly to Python: the method is a function owned by the class, and its argument is an instance of the class. But you should not call instance methods this way.

    The best way to solve this problem is to construct a Location object and then call the method on it:

    let city: Location = Location().getCity()
    
    0 讨论(0)
提交回复
热议问题