Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/44.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 将函数中的值设置为函数外的变量(Swift)_Ios_Iphone_Swift_Cllocation - Fatal编程技术网

Ios 将函数中的值设置为函数外的变量(Swift)

Ios 将函数中的值设置为函数外的变量(Swift),ios,iphone,swift,cllocation,Ios,Iphone,Swift,Cllocation,我想获取newRecordLat和newRecordLong,并将它们保存为函数外部的变量。其目的是因为我想将这些变量存储在数据库中。它们当前以CLLocationDegrees输出,而不是字符串、int、float或double。我确实在什么地方读到过CLLocationDegrees是双学位 func textFieldShouldReturn(textField: UITextField!) -> Bool { localSearchRequest = MKLocalSea

我想获取newRecordLat和newRecordLong,并将它们保存为函数外部的变量。其目的是因为我想将这些变量存储在数据库中。它们当前以CLLocationDegrees输出,而不是字符串、int、float或double。我确实在什么地方读到过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
变量声明为类的属性。为此,请将这两个声明移出
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
}