如何访问块外的变量?(复制)

时间:2023-01-24 18:30:57

This question already has an answer here:

这个问题已经有了答案:

Following code doesn't work properly.

下面的代码不能正常工作。

  func convertToStreet(location:CLLocationCoordinate2D) -> CLPlacemark {

    var tempLocation = CLLocation(latitude: location.latitude, longitude: location.longitude)

    var temPlacemark:CLPlacemark?

    CLGeocoder().reverseGeocodeLocation(tempLocation, completionHandler: {(placemarks, error) in

        temPlacemark = (placemarks[0] as CLPlacemark)
        println(temPlacemark!.thoroughfare)

    })

    return temPlacemark!
}

Println inside the completion handler works properly, but the value of temPlacemark is nil at the end of code. Why does that happen? I thank you very much in advance.

完成处理程序内部的Println工作正常,但是temPlacemark的值在代码末尾为nil。为什么会这样呢?我提前非常感谢你。

2 个解决方案

#1


2  

It's because that completionHandler is called asynchronously. For that to work you should have a callback block in your custom function to return the value once you get it from the CLGeocoder.

因为这个completionHandler被异步调用。要实现这一点,您应该在自定义函数中有一个回调块,以便在从CLGeocoder获得值后返回该值。

Something like this:

是这样的:

func convertToStreet(coordinate: CLLocationCoordinate2D, completionHandler: (placemark: CLPlacemark!, error: NSError!) -> Void) {
    let tempLocation = CLLocation(latitude: coordinate.latitude, longitude: coordinate.longitude)

    CLGeocoder().reverseGeocodeLocation(tempLocation) { placemarks, error in
        completionHandler(placemark: placemarks?.first as CLPlacemark?, error: error)
    }
}

You'd then call it like so:

你会这样称呼它:

convertToStreet(location.coordinate) { placemark, error in
    if placemark != nil {
        // use `placemark` here
        println(placemark.thoroughfare)
    } else {
        println(error)
    }
}

// but don't use `placemark` here

#2


0  

The problem of your code is that the block code :

您的代码的问题是块代码:

temPlacemark = (placemarks[0] as CLPlacemark)
println(temPlacemark!.thoroughfare)

will be executed later.

将在稍后执行。

Meaning that the current return statement will always return an uninitialized value. If you want to initialize this var in your block you should make it a property of your object.

意味着当前返回语句将始终返回未初始化的值。如果您想在您的块中初始化这个var,您应该将它作为对象的属性。

#1


2  

It's because that completionHandler is called asynchronously. For that to work you should have a callback block in your custom function to return the value once you get it from the CLGeocoder.

因为这个completionHandler被异步调用。要实现这一点,您应该在自定义函数中有一个回调块,以便在从CLGeocoder获得值后返回该值。

Something like this:

是这样的:

func convertToStreet(coordinate: CLLocationCoordinate2D, completionHandler: (placemark: CLPlacemark!, error: NSError!) -> Void) {
    let tempLocation = CLLocation(latitude: coordinate.latitude, longitude: coordinate.longitude)

    CLGeocoder().reverseGeocodeLocation(tempLocation) { placemarks, error in
        completionHandler(placemark: placemarks?.first as CLPlacemark?, error: error)
    }
}

You'd then call it like so:

你会这样称呼它:

convertToStreet(location.coordinate) { placemark, error in
    if placemark != nil {
        // use `placemark` here
        println(placemark.thoroughfare)
    } else {
        println(error)
    }
}

// but don't use `placemark` here

#2


0  

The problem of your code is that the block code :

您的代码的问题是块代码:

temPlacemark = (placemarks[0] as CLPlacemark)
println(temPlacemark!.thoroughfare)

will be executed later.

将在稍后执行。

Meaning that the current return statement will always return an uninitialized value. If you want to initialize this var in your block you should make it a property of your object.

意味着当前返回语句将始终返回未初始化的值。如果您想在您的块中初始化这个var,您应该将它作为对象的属性。