2016-03-23 1 views
0

Ich habe eine Klasse mit dem Namen Location, die mehrere Methoden enthält, die keine Parameter haben.Eine Methode ohne Parameter ruft ein Argument auf

Wenn ich jedoch versuche, eine Variable mit dem Ergebnis der Methode zu erstellen, will es ein Argument. Warum das?

Location Klasse:

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 
    } 
} 

Der Versuch, eine Variable zu erstellen:

let city = Location.getCity() 

Hier sind einige Screenshots von dem, was ich bekommen:

enter image description here

enter image description here

Antwort

2

Diese Methoden sind keine Klassenmethoden, sie sind Instanzmethoden. Sie müssen sie für eine Instanz der Klasse Location aufrufen, nicht für die Klasse selbst. Offensichtlich kann Swift Instanzmethoden ähnlich wie Python aufrufen: Die Methode ist eine Funktion, die der Klasse gehört, und ihr Argument ist eine Instanz der Klasse. Aber Sie sollten Instanzmethoden nicht auf diese Weise aufrufen.

Der beste Weg, um dieses Problem zu lösen, ist ein Location Objekt zu konstruieren und dann auf sie rufen Sie die Methode:

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

Weil Sie versuchen, es als eine Klassenfunktion zu nennen. Sie sollten eine Instanz von Location erstellen und die Funktion darauf aufrufen. Beachten Sie auch, dass es String zurückgibt, in dem Ihr Code dem Compiler mitteilt, dass Sie erwarten, dass es einen Location zurückgibt.