2016-03-23 29 views
0

パラメータがないメソッドがいくつかある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

これらのメソッドはクラスメソッドではなく、インスタンスメソッドです。クラス自体ではなく、Locationクラスのインスタンスで呼び出す必要があります。明らかに、SwiftはPythonと同様にインスタンスメソッドを呼び出すことができます。メソッドはクラスが所有する関数であり、引数はクラスのインスタンスです。しかし、インスタンスメソッドをこのように呼び出すべきではありません。

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

この問題を解決するための最良の方法は、Locationオブジェクトを構築し、それに対してメソッドを呼び出すことです

1

あなたはそれをクラス関数として呼び出そうとしているからです。 Locationのインスタンスを作成し、その上で関数を呼び出す必要があります。また、Stringが返されることに注意してください。コードでは、コンパイラに対して、Locationを返すと予想しています。

関連する問題