2016-07-02 2 views
0

У меня есть эта функция:MapKit Swift Преобразование адреса в координированного

func addressToCoordinatesConverter(address: NSString) { 
    let geoCoder = CLGeocoder() 
    geoCoder.geocodeAddressString(address, completionHandler: { (placemarks, error) -> Void in 
     if error = nil { 
      if placemarks!.count = 0 { 
       let annotation = MKAnnotation() 
       annotation.coordinate = (placemark.location?.coordinate)! 
       self.mapView.showAnnotations([annotation], animated: true) 
       self.mapView.selectedAnnotations(annotation, animated: true) 
      } 
     } 
    }) 

я называю это в моем коде для преобразования адреса (строка «адрес») в координаты, а затем на мой MAPview. Я получаю сообщение об ошибке в третьей строке здесь:

"NSString is not implicitly convertible to 'String'; did you mean to use 'as' to explicitly convert" 

Как лучше всего исправить эту ошибку? Благодаря

+0

как ошибка. почему вы не используете String вместо функции? func addressToCoordinatesConverter (адрес: String) –

ответ

1
func addressToCoordinatesConverter(address: String) { 
     let geoCoder = CLGeocoder() 
     geoCoder.geocodeAddressString(address, completionHandler: { (placemarks, error) -> Void in 
      if error = nil { 
       if placemarks!.count = 0 { 
        let annotation = MKAnnotation() 
        annotation.coordinate = (placemark.location?.coordinate)! 
        self.mapView.showAnnotations([annotation], animated: true) 
        self.mapView.selectedAnnotations(annotation, animated: true) 
       } 
      } 
     }) 

или

func addressToCoordinatesConverter(address: NSString) { 
    let geoCoder = CLGeocoder() 
    geoCoder.geocodeAddressString(address as! String, completionHandler: { (placemarks, error) -> Void in 
     if error = nil { 
      if placemarks!.count = 0 { 
       let annotation = MKAnnotation() 
       annotation.coordinate = (placemark.location?.coordinate)! 
       self.mapView.showAnnotations([annotation], animated: true) 
       self.mapView.selectedAnnotations(annotation, animated: true) 
      } 
     } 
    }) 

первый вы передаете его в строку, а второй NSString. Используйте тот, который совпадает с тем, что вы хотите передать.

Смежные вопросы