2016-07-19 40 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"と出てきます。 2つのジオコーディングライン(self.forwardGeocoding)&(self.forwardGeocodingDesired)は問題ではありませんが、正常に動作しますが、正しいDouble値が表示されない理由はわかりません。どんな提案も大変ありがとうございます。ありがとうございます。swift関数の戻り値が正しく返されない

答えて

0

self.forwardGeocodingとself.forwardGeocodingDesiredの完了ブロックが非同期で実行されているかどうかを確認してください。その場合、値は印刷が実行されるまで設定されません。

+0

彼らは非同期に実行します。あなたが気にしないなら、どうすればこの問題を解決できますか? –

1

forwardGeocodingおよびforwardGeocodingDesiredは、実行に時間がかかるネットワーク上の操作を呼び出します。これはUIをブロックしないようにバックグラウンドスレッドを使用して行われます。操作が完了すると、完了クロージャのコードが実行されます。操作が完了しており、これらの変数があなたの閉鎖によって設定されているので、前に前に

あなたgetLatsAndLongs機能がlatlongdesiredLatdesiredLongを返します。

完了ハンドラをgetLatsAndLongsに渡すと、操作が完了した後に呼び出すことができますが、2つのジオコーディング操作を並行して実行していて、どちらが先に終了するのかわからないため状況は複雑です。最初のコンプリートハンドラから2番目のハンドラをディスパッチできますが、これは遅くなります。より良いアプローチは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

おそらく何かがnilで、あなたはそれをアンラップします。また、私は 'forwardGeocodingDesired'と' forwardGeocoding'のソースを持っていないので、 'coordinate'はCLLocationCoordinate2Dであると仮定しました - これは間違っているかもしれず、それに応じて調整するべきです – Paulw11

+0

クラッシュはどのラインで起こりましたか?例外ブレークポイントを設定してみてください – Paulw11

関連する問題