2016-12-12 5 views
-1
Reverse Geocode Response = { 
    error = "Unable to geocode"; 
} 

Reverse Geocode Response = { 
    address =  { 
     city = "San Francisco"; 
     country = "United States of America"; 
     "country_code" = us; 
     county = "San Francisco City and County"; 
     electronics = "Apple Store"; 
     neighbourhood = Chinatown; 
     pedestrian = "Stockton Street"; 
0postcode = 94104; 
     state = California; 
    }; 
    boundingbox =  (
     "37.7857515", 
     "37.7859644", 
     "-122.4066919", 
     "-122.406347" 
    ); 
    "display_name" = "Apple Store, Stockton Street, Chinatown, San Francisco, San Francisco City and County, California, 94104, United States of America"; 
    lat = "37.7858585"; 
    licence = "Data \U00a9 OpenStreetMap contributors, ODbL 1.0. http://www.openstreetmap.org/copyright"; 
    lon = "-122.406519260264"; 
    "osm_id" = 147689077; 
    "osm_type" = way; 
    "place_id" = 97545943; 
} 

i Erfolg und scheitern Antwort von APIwie sie beheben Json in schnellen 2,3 und XCode 8,1

do { 
       let result = try? NSJSONSerialization.JSONObjectWithData(response, options:.AllowFragments) 
       // print("Reverse Geocode Response = \(result) ") 

       if result != nil { 

        let address:Dictionary = result!["address"] as! Dictionary<String,AnyObject> 
        if address.count > 0 { 

         country = address["country"] as! String //country_code 
         postalCode = address["postcode"] as! String 

         print("Country = \(country) ,Postal Code = \(postalCode)") 

        } 
       }else{ 
        print("Error") 
       } 

      }catch{ 
       print("Error with Json \(error)") 
      } 

ich bekam Absturz gescheitert Absturz Reaktionszeit bekam abstürzt. bitte helfen, diesen Absturz zu finden.

wenn ich bekam error = "Geocode nicht möglich"; Diese Antwort bekam ich Fehler WIE diese Art von Situation zu lösen Danke im Vorraus

+0

können Sie die Fehlermeldung posten? –

+2

Denken Sie daran, jede Verwendung von '!' In Ihrem Code bedeutet "hier abstürzen". – rmaddy

Antwort

0

Nie ! und as! verwenden, wenn Sie ganz sicher sind Sie die genauen Typen haben Sie werfen in oder die Werte unter den ! sind nicht gleich Null. In Ihrem Beispiel haben Sie as! überall, um Werte aus dem Wörterbuch zu erhalten, während Sie diese Werte nicht haben, so dass alles abstürzt. Ein richtiger Weg wäre:

// Ensure you have a valid json and the result is not empty 
guard let json = try? NSJSONSerialization.JSONObjectWithData(response, options:.AllowFragments), 
result = json as? [String: Any] where !result.isEmpty else { 
print("Error while parsing \(response)") // Parsing to json failed 
    return 
} 
// Try to get address out 
guard let address = json["address"] as? [String: Any] where  !address.isEmpty, 
    country = address["Country"] as? String, 
    postalCode = address["postcode"] as? String else { 
    print("Error") // There is no address, country or postcode 
    return 
} 
// Here you have your address, country and postalCode