2017-12-25 8 views
0

私はスピーディーで新しく、クロージャとクロージャのコンセプトをどのように処理するのか分かりません。スウィフトでのクロージャの操作

最近私はquestionと頼みました。geocodeAddressStringが非同期で実行されるので、このプロパティが最終的に設定される前にlatLongが実行されるため、変数がnilであることがわかりました。

import UIKit 
import CoreLocation 
import Firebase 

var latLong: String! 

override func viewDidLoad() { 
    super.viewDidLoad() 

} 

func findCordiante(adress:String){ 

    let geocoder = CLGeocoder() 
    geocoder.geocodeAddressString(adress) { 
     placemarks, error in 

     if (placemarks != nil){ 
      let placemark = placemarks?.first 
      let lat = placemark?.location?.coordinate.latitude 
      let lon = placemark?.location?.coordinate.longitude 

      self.latLong = String(describing: lat!) + "," + String(describing: lon!) 

     }else{ 
      //handle no adress 
      self.latLong = "" 
     } 
    } 

} 

@IBAction func createSchool(_ sender: Any) { 

     //when user press button i want execute function and assign value to variable latLong 
     findCordiante(adress: "Cupertino, California, U.S.") 
     //so then I need to manipulate with that value here, let's say example 

     //example 
     print("Hi user, your coordinates is \(latLong)") 

} 

私はクロージャ内print(latLong)を追加すると、それは印刷が、私はクロージャ内のすべての機能を実行するためにたくない次のとおりです。ここ

しかし、私は理解できない新しい質問です。私はその後、私はクラス

+0

[Swift closure async order of execution]の複製が可能です(https://stackoverflow.com/questions/39880506/swift-closure-async-order-of-execution) –

+0

できません。あなたは結果を待つ必要があります –

答えて

関連する問題