将函数中的值设置为函数外的变量 (Swift)
Setting a value in a function as a variable outside the function (Swift)
我想获取 newRecordLat 和 newRecordLong 并将它们保存为函数外的变量。目的是因为我想将这些变量存储在数据库中。它们当前以 CLLocationDegrees 输出,而不是字符串、整数、浮点数或双精度数。我确实在某处读到 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
}
//GRAB newRecordLat and newRecordLong AND STORE THEM OUTSIDE THE FUNCTION
var newRecordLat = localSearchResponse.boundingRegion.center.latitude
var newRecordLong = localSearchResponse.boundingRegion.center.longitude
}
您应该将 newRecordLat
和 newRecordLong
变量声明为 class 的属性。为此,将两个声明移到 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
}
我想获取 newRecordLat 和 newRecordLong 并将它们保存为函数外的变量。目的是因为我想将这些变量存储在数据库中。它们当前以 CLLocationDegrees 输出,而不是字符串、整数、浮点数或双精度数。我确实在某处读到 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
}
//GRAB newRecordLat and newRecordLong AND STORE THEM OUTSIDE THE FUNCTION
var newRecordLat = localSearchResponse.boundingRegion.center.latitude
var newRecordLong = localSearchResponse.boundingRegion.center.longitude
}
您应该将 newRecordLat
和 newRecordLong
变量声明为 class 的属性。为此,将两个声明移到 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
}