2016-08-12 6 views
2

Ich versuche, eine Zeichenfolge, die ich aus Firebase abruft, zu konvertieren und sie als mehrere Anmerkungen in Google Maps hinzuzufügen. Unfortuanately, mein App abstürzt, wenn es durch den aktuellen Code geht:So wandeln Sie String in CLLocationDegrees um Swift 2

ref = FIRDatabase.database().reference() 
    ref.child("Locations").observeSingleEventOfType(.Value, withBlock: { (snapshot) in 
     let lat = (snapshot.value!["Latitude"] as! NSString).doubleValue 
     let lon = (snapshot.value!["Longitude"] as! NSString).doubleValue 



     let complainLoc = CLLocationCoordinate2DMake(lat, lon) 

     let Coordinates = CLLocationCoordinate2D(latitude: lat, longitude: lon) 

    }) 

My JSON Tree

My Code Block Which Crashes

Hier den Code I zum Speichern von Daten

FIRDatabase.database().reference().child("Location").child(FIRAuth.auth()!.currentUser!.uid).setValue(["Latitude": locationManager.location!.coordinate.latitude, "Longitude": locationManager.location!.coordinate.longitude]) 
+0

Sind Sie sicher, dass es abstürzt ist, wo es die 'CLLocationCoordinate2D' ist zu schaffen und nicht, wo Sie diese gezwungen Casting tun/auspacken? Überprüfen Sie, ob "value" nicht "nil" ist. Überprüfen Sie auch, dass 'value [" Latitude "]' und 'value [" Longitude "]' sowohl (a) nicht "nil", als auch (b) String-Werte sind. Mit all diesen "!" Erzwungenen Auspackern und erzwungenen Casting-Operatoren gibt es hier viele mögliche Absturzquellen. – Rob

+0

Wofür steht 'meckern'? – vadian

Antwort

2

Fabrikat zu Firebase Sicher, wenn Sie die Werte lat und lon in der Datenbank speichern, sind Sie s achdem sie als Float oder Doppel .. Zum Abrufen Verwendung:

ref = FIRDatabase.database().reference() 
    ref.child("Locations").observeEventType(.Value, withBlock: { (snapshot) in 

    if snapshot.exists(){  

    if let locationDictionary = snapshot.value as [String : AnyObject]{ 

     for each in locationDictionary{   

    //each will bring you every location dictionary in your database for your every user 
      let lat = each.value!["Latitude"] as! CLLocationDegrees 
      let lon = each.value!["Longitude"] as! CLLocationDegrees 
      let userId = each.key as! String 

      let complainLoc = CLLocationCoordinate2DMake(lat, lon) 

      let Coordinates = CLLocationCoordinate2D(latitude: lat, longitude: lon) 
     //Every time this for loop complete's itself it will generate a new set of Coordinates for each user 
     } 
     } 
    } 
}) 
+0

Die App stürzt immer noch richtig ab, wenn ich die Let-Variablen lat und lon erstelle. Ich habe Latitude und Lngitude korrekt eingegeben, genauso wie beim Speichern von Daten in Firebase. –

+0

Ich habe meinen Beitrag aktualisiert –

+0

Dieser Code funktioniert, aber ich möchte alle Standorte von allen separaten UIDs (Benutzer) abrufen, aber wenn ich das Kind der aktuellen Benutzer-UID entfernen, stürzt es –

Verwandte Themen