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 值。如有任何建议,我们将不胜感激,谢谢。
最佳答案
forwardGeocoding
和 forwardGeocodingDesired
通过网络调用需要时间执行的操作。这是使用后台线程完成的,以免阻塞 UI。当操作完成时,将执行完成闭包中的代码。
您的 getLatsAndLongs
函数返回 lat
、long
、desiredLat
和 desiredLong
之前操作已经完成,因此在您的闭包设置这些变量之前。
您可以将完成处理程序传递给 getLatsAndLongs
以在操作完成后调用,但您的情况很复杂,因为您并行执行两个地理编码操作,并且您不知道哪个会先完成。您可以从第一个的完成处理程序中分派(dispatch)第二个,但这会更慢。更好的方法是使用 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)
}
关于ios - swift 函数返回值未正确返回,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38448060/