0

how to fix this error :

"downcast from CLplacemark? to Clplacemark only unwraps optional"

so i use this code :

if let p = CLPlacemark(placemark: placemarks.first as? CLPlacemark){}

and change placemarks[0] array but not work

and my code is :

CLGeocoder().reverseGeocodeLocation(userLocation) { (placemarks, error) -> Void in

    if error == nil {

       if let p = CLPlacemark(placemark: placemarks[0] as? CLPlacemark){

            print(p)
            self.adsresslabel.text = "\(p.administrativeArea)\(p.postalCode)\(p.country)"
        }


    }else {
        print (error)
    }


    }

at this code :

if let p = CLPlacemark(placemark: placemarks[0] as? CLPlacemark)

i have error about

 "downcast from CLplacemark? to Clplacemark only unwraps optional"

how to fix this error ?!

vadian
  • 274,689
  • 30
  • 353
  • 361
user1707895
  • 13
  • 1
  • 4

2 Answers2

0

If you don't actually need to make a copy of the first placemark, you only need to do :

if let p = placemarks.first
{
        print(p)
        self.adsresslabel.text = "\(p.administrativeArea)\(p.postalCode)\(p.country)"
}

On the other hand, if you do need it to be a copy:

if let p0 = placemarks.first
{ 
        let p = CLPlacemark(placemark:p0)
        print(p)
        self.adsresslabel.text = "\(p.administrativeArea)\(p.postalCode)\(p.country)"
}
Alain T.
  • 40,517
  • 4
  • 31
  • 51
0

The completion handler of reverseGeocodeLocation:completionHandler is declared as

typealias CLGeocodeCompletionHandler = ([CLPlacemark]?, NSError?) -> Void

The placemarks are an optional array of CLPlacemark items therefore a down cast is not needed. You have only to check if the array is not nil and not empty as described in the answer of Alain T.

vadian
  • 274,689
  • 30
  • 353
  • 361