没有参数的方法是调用参数

时间:2016-03-23 19:57:52

标签: swift class parameters arguments

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

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

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

2 个答案:

答案 0 :(得分:2)

这些方法不是类方法,它们是实例方法。您必须在Location类的实例上调用它们,而不是在类本身上调用它们。显然,Swift可以类似于Python调用实例方法:该方法是类所拥有的函数,其参数是类的实例。但是你不应该这样调用实例方法。

解决此问题的最佳方法是构造一个Location对象,然后在其上调用该方法:

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

答案 1 :(得分:1)

因为您试图将其称为类函数。您应该创建Location的实例并在其上调用该函数。另请注意,它返回String您的代码告诉编译器您希望它返回Location