A method without parameters is calling for an argument

后端 未结 2 980
野的像风
野的像风 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: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()
    

提交回复
热议问题