swift - 没有参数的方法正在调用参数

标签 swift class parameters arguments

我有一个名为 Location 的类,其中有几个没有任何参数的方法。

但是,当我尝试使用该方法的结果创建一个变量时,它需要一个参数。这是为什么?

位置类:

let locationManager = CLLocationManager()

public class Location {

    public func coordinate() -> (latitude: Float?, longitude: Float?) {
        let latitude = Float((locationManager.location?.coordinate.latitude)!)
        let longitude = Float((locationManager.location?.coordinate.longitude)!)

        return (latitude: latitude, longitude: longitude)
    }

    public func getCity() -> String {
        var returnCity: String = "N/A"
        let geoCoder = CLGeocoder()
        let location = CLLocation(latitude: (locationManager.location?.coordinate.latitude)!, longitude: (locationManager.location?.coordinate.longitude)!)

        geoCoder.reverseGeocodeLocation(location, completionHandler: { (placemarks, error) -> Void in
            // Place details
            var placeMark: CLPlacemark!
            placeMark = placemarks?[0]
            // City
            if let city = placeMark.addressDictionary!["City"] as? String {
                returnCity = city
            }
        })
        return returnCity
    }

    public func getCountry() -> String {
        var returnCountry: String = "N/A"
        let geoCoder = CLGeocoder()
        let location = CLLocation(latitude: (locationManager.location?.coordinate.latitude)!, longitude: (locationManager.location?.coordinate.longitude)!)

        geoCoder.reverseGeocodeLocation(location, completionHandler: { (placemarks, error) -> Void in
            // Place details
            var placeMark: CLPlacemark!
            placeMark = placemarks?[0]
            // City
            if let country = placeMark.addressDictionary!["Country"] as? String {
                returnCountry = country
            }
        })
        return returnCountry
    }

    public func getZip() -> Int {
        var returnZip: Int = 0
        let geoCoder = CLGeocoder()
        let location = CLLocation(latitude: (locationManager.location?.coordinate.latitude)!, longitude: (locationManager.location?.coordinate.longitude)!)

        geoCoder.reverseGeocodeLocation(location, completionHandler: { (placemarks, error) -> Void in
            // Place details
            var placeMark: CLPlacemark!
            placeMark = placemarks?[0]
            // City
            if let zip = placeMark.addressDictionary!["ZIP"] as? Int {
                returnZip = zip
            }
        })
        return returnZip
    }

    public func getLocationName() -> String {
        var returnName: String = "N/A"
        let geoCoder = CLGeocoder()
        let location = CLLocation(latitude: (locationManager.location?.coordinate.latitude)!, longitude: (locationManager.location?.coordinate.longitude)!)

        geoCoder.reverseGeocodeLocation(location, completionHandler: { (placemarks, error) -> Void in
            // Place details
            var placeMark: CLPlacemark!
            placeMark = placemarks?[0]
            // City
            if let locationName = placeMark.addressDictionary!["Name"] as? String {
                returnName = locationName
            }
        })
        return returnName
    }

    public func getStreetAddress() -> String {
        var returnAddress: String = "N/A"
        let geoCoder = CLGeocoder()
        let location = CLLocation(latitude: (locationManager.location?.coordinate.latitude)!, longitude: (locationManager.location?.coordinate.longitude)!)

        geoCoder.reverseGeocodeLocation(location, completionHandler: { (placemarks, error) -> Void in
            // Place details
            var placeMark: CLPlacemark!
            placeMark = placemarks?[0]
            // City
            if let street = placeMark.addressDictionary!["Thoroughfare"] as? String {
                returnAddress = street
            }
        })
        return returnAddress
    }
}

尝试创建一个变量:

let city = Location.getCity()

以下是我得到的一些屏幕截图:

enter image description here

enter image description here

最佳答案

这些方法不是类方法,它们是实例方法。您必须在 Location 类的实例上调用它们,而不是在类本身上。可见,Swift 可以像 Python 一样调用实例方法:方法是类拥有的函数,它的参数是类的实例。但是您不应该以这种方式调用实例方法。

解决这个问题最好的方法是构造一个Location对象,然后调用它的方法:

let city: Location = Location().getCity()

关于swift - 没有参数的方法正在调用参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36187567/

相关文章:

ios - 委托(delegate)不适用于 SplitView

class - 如何使用 UML 图显示哪个类的变量用于哪个类的操作?

python - 在 __init__ 中获取有序字典类属性

c - 具有最小参数的可变参数函数

javascript - 将对象传递给函数

ios - 无法分配给属性 : 'item' is a 'let' constant (Swift 4)

ios - 在 Mac 上使用 SwiftUI App Lifecycle 时,@UIApplicationMain 只能用于 'class' 声明错误

ios - 如何正确配置 MMDrawerController

javascript - 类语法 : React Component Fires the OnChange within Closure

c - C 中参数的顺序