Ios 将函数中的值设置为函数外的变量(Swift)
我想获取newRecordLat和newRecordLong,并将它们保存为函数外部的变量。其目的是因为我想将这些变量存储在数据库中。它们当前以CLLocationDegrees输出,而不是字符串、int、float或double。我确实在什么地方读到过CLLocationDegrees是双学位Ios 将函数中的值设置为函数外的变量(Swift),ios,iphone,swift,cllocation,Ios,Iphone,Swift,Cllocation,我想获取newRecordLat和newRecordLong,并将它们保存为函数外部的变量。其目的是因为我想将这些变量存储在数据库中。它们当前以CLLocationDegrees输出,而不是字符串、int、float或double。我确实在什么地方读到过CLLocationDegrees是双学位 func textFieldShouldReturn(textField: UITextField!) -> Bool { localSearchRequest = MKLocalSea
func textFieldShouldReturn(textField: UITextField!) -> Bool {
localSearchRequest = MKLocalSearchRequest()
localSearchRequest.naturalLanguageQuery = addressTextField.text
println(addressTextField.text)
println("addressTextField.text^^")
localSearch = MKLocalSearch(request: localSearchRequest)
localSearch.startWithCompletionHandler { (localSearchResponse, error) -> Void in
println(localSearchResponse)
println("localSearchResponse^^")
if localSearchResponse == nil{
var alert = UIAlertView(title: nil, message: "Place not found", delegate: self, cancelButtonTitle: "Try again")
alert.show()
return
}
//GRAB newRecordLat and newRecordLong AND STORE THEM OUTSIDE THE FUNCTION
var newRecordLat = localSearchResponse.boundingRegion.center.latitude
var newRecordLong = localSearchResponse.boundingRegion.center.longitude
}
您应该将
newRecordLat
和newRecordLong
变量声明为类的属性。为此,请将这两个声明移出textFieldShouldReturn(:)
class MyClass: UITextFieldDelegate {
var newRecordLat: CLLocationDegrees?
var newRecordLong: CLLocationDegrees?
...
...
func textFieldShouldReturn(textField: UITextField!) -> Bool {
localSearchRequest = MKLocalSearchRequest()
localSearchRequest.naturalLanguageQuery = addressTextField.text
println(addressTextField.text)
println("addressTextField.text^^")
localSearch = MKLocalSearch(request: localSearchRequest)
localSearch.startWithCompletionHandler { (localSearchResponse, error) -> Void in
println(localSearchResponse)
println("localSearchResponse^^")
if localSearchResponse == nil {
var alert = UIAlertView(title: nil, message: "Place not found", delegate: self, cancelButtonTitle: "Try again")
alert.show()
return
}
self.newRecordLat = localSearchResponse.boundingRegion.center.latitude
self.newRecordLong = localSearchResponse.boundingRegion.center.longitude
}