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
)不是问题,它们工作正常,但我不知道他们为什么不打印正确的Double值。非常感谢任何建议,谢谢。
答案 0 :(得分:1)
forwardGeocoding
和forwardGeocodingDesired
通过网络调用一个需要时间执行的操作。这是使用后台线程完成的,以便不阻止UI。当操作完成时,执行完成闭包中的代码。
您的getLatsAndLongs
函数在操作完成之前返回lat
,long
,desiredLat
和desiredLong
,因此在您的闭包设置这些变量之前
您可以将完成处理程序传递给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)
}
答案 1 :(得分:0)
检查self.forwardGeocoding和self.forwardGeocodingDesired中的完成阻止是否异步执行。如果是这样,则在打印完毕之后才会设置这些值。