2016-07-19 162 views
0
func getLatsAndLongs() -> (LATITUDE: Double, LONGITUDE: Double, DESIREDLAT: Double, DESIREDLONG: Double) { 



    self.forwardGeocoding("\(addressTxtFld.text) \(cityTxtFld.text), \(stateTxtFld.text)", completion: { 
     success, coordinate in 

     if success { 
      self.lat = coordinate.latitude 
      self.long = coordinate.longitude 

      print("\(self.lat) is the latitude for the initial location") 
      print("\(self.long) is the longitude for the initial location") 

      self.INITIAL_DESTINATION_LATITUDE = self.lat 
      self.INITIAL_DESTINATION_LONGITUDE = self.long 

      var initialLocation = CLLocationCoordinate2DMake(self.INITIAL_DESTINATION_LATITUDE, self.INITIAL_DESTINATION_LONGITUDE) 



     } else { 
      print("Error at forwardGeocoding @willcohen @ERRORALERT") 
     } 
    }) 


    self.forwardGeocodingDesired("\(addressTxtFldDest.text) \(cityTxtFldDest.text), \(stateTxtFldDest.text)", completion: { 
     success, coordinate in 

     if success { 
      self.desiredLat = coordinate.latitude 
      self.desiredLong = coordinate.longitude 

      print("\(self.desiredLat) is the latitude for the desired location") 
      print("\(self.desiredLong) is the longitude for the desired locaiton") 

      self.DESIRED_DESTIANTION_LATITUDE = self.desiredLat 
      self.DESIRED_DESTINATION_LONGITUDE = self.desiredLong 

      var desiredLocation = CLLocationCoordinate2DMake(self.DESIRED_DESTIANTION_LATITUDE, self.DESIRED_DESTINATION_LONGITUDE) 




     } else { 
      print("Error at forwardGeocodingDesired @willcohen @ERRORALERT") 
     } 

    }) 

    return (lat,long,desiredLat,desiredLong) 

} 


      let latsAndLongs = getLatsAndLongs() 

     let latFF = latsAndLongs.LATITUDE 
     let longFF = latsAndLongs.LONGITUDE 
     let latDFF = latsAndLongs.DESIREDLAT 
     let longDFF = latsAndLongs.DESIREDLONG 

     print("\(latFF) final lat") 
     print("\(longFF) final long") 
     print("\(latDFF) final latD") 
     print("\(longDFF) final longD") 

好。所以,當我嘗試打印最後4行的所有行時,它每次都顯示爲「0」。請注意,這兩個地理編碼線(self.forwardGeocoding)&(self.forwardGeocodingDesired)不是問題,他們的工作很好,但我不知道他們爲什麼不打印正確的雙重價值。任何建議將不勝感激,謝謝。迅速函數的返回值不返回正確

回答

0

檢查中self.forwardGeocoding和self.forwardGeocodingDesired完成塊異步執行。如果是這樣,這些值將在打印完成之後纔會被設置。

+0

他們異步執行。如果你不介意,我會如何解決這個問題? –

1

forwardGeocodingforwardGeocodingDesired調用操作在其花費時間來執行該網絡。這是使用後臺線程完成的,以免阻塞UI。當操作完成完成閉包中的代碼時執行。

getLatsAndLongs功能相關的操作已經完成,因此前這些變量已經被關閉的設置之前返回latlongdesiredLatdesiredLong

您可以通過完成處理程序getLatsAndLongs要調用的操作完成後,而是因爲你在並行執行兩個地理編碼操作你的情況是複雜的,你不知道這將首先完成。你可以從第一個的完成處理程序派發第二個,但是這會更慢。更好的方法是使用dispatch_group:

func getLatsAndLongs(completion:(initialLocation: CLLocationCoordinate2D?, desiredLocation: CLLocationCoordinate2D?)->Void) { 

    let dispatchGroup = dispatch_group_create() 

    var initialLocation: CLLocationCoordinate2D? 
    var desiredLocation: CLLocationCoordinate2D? 

    dispatch_group_enter(dispatchGroup) 

    self.forwardGeocoding("\(addressTxtFld.text) \(cityTxtFld.text), \(stateTxtFld.text)", completion: { 
     success, coordinate in 
     if success { 
      initialLocation = coordinate 
     } else { 
      print("Error at forwardGeocoding @willcohen @ERRORALERT") 
     } 
     dispatch_group_leave(dispatchGroup) 
    }) 


    self.forwardGeocodingDesired("\(addressTxtFldDest.text) \(cityTxtFldDest.text), \(stateTxtFldDest.text)", completion: { 
     success, coordinate in 

     if success { 
      desiredLocation = coordinate 
     } else { 
      print("Error at forwardGeocodingDesired @willcohen @ERRORALERT") 
     } 
     dispatch_group_leave(dispatchGroup) 
    }) 

    dispatch_group_notify(dispatchGroup, dispatch_get_main_queue()) { 
     completion(initialLocation:initialLocation,desiredLocation:desiredLocation) 
    } 
} 

用法:

getLatsAndLongs { (initialLocation, desiredLocation) in 
    print(initialLocation) 
    print(desiredLocation) 
} 
+0

我做了你所做的編輯,然後像你說的那樣調用它,但是在我的appDelegate.swift中出現了錯誤,它是'線程1:EXC_BAD_INSTRUCTION(代碼= EXC_I386_INVOP,子代碼= 0x0)' –

+0

可能是零和你強行解開它。另外,因爲我沒有'forwardGeocodingDesired'和'forwardGeocoding'的源碼,我假定'coordinate'是一個CLLocationCoordinate2D - 這可能是不正確的,你應該相應地進行調整。 – Paulw11

+0

哪條線發生了崩潰?嘗試設置一個異常斷點 – Paulw11