function - 快速访问函数外部的变量

标签 function swift global-variables cllocation cllocationcoordinate2d

我从 获取坐标,但我希望能够在位置管理器功能之外使用它们。是否可以将它们设为实例变量?如果是这样,有人能给我一些示例代码吗?在下面的代码中,我在 func 内打印并且它可以工作,但我希望能够在其外部打印,或者使用坐标。也许作为全局变量?我将如何编码?我无法从文档中完全弄清楚。

import UIKit
import CoreLocation
import MapKit

class ViewController: UIViewController, CLLocationManagerDelegate, MKMapViewDelegate {

  var locationManager: CLLocationManager!
  var seenError : Bool = false
  var theSpan: MKCoordinateSpan = MKCoordinateSpanMake(0.1, 0.1)
  var locationStatus : NSString = "Not Started"

  var initialLoc:Int = 1

  override func viewDidLoad() {
    super.viewDidLoad()

    locationManager = CLLocationManager()
    locationManager.requestAlwaysAuthorization()
    locationManager.delegate = self
    var initialLoc:Int = 1
    // Do any additional setup after loading the view, typically from a nib.
  }

  @IBOutlet weak var Map: MKMapView!
  @IBAction func Here(sender: AnyObject) {       
    initLocationManager()
  }

  func initLocationManager() {
    seenError = false
    locationManager = CLLocationManager()
    locationManager.delegate = self
    locationManager.startUpdatingLocation()
    Map.showsUserLocation = true
  }

  func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) {

    var locationArray = locations as NSArray
    var locationObj = locationArray.lastObject as CLLocation
    var coord = locationObj.coordinate
    if initialLoc == 1 {
        var Region:MKCoordinateRegion = MKCoordinateRegionMake(coord, theSpan)
        self.Map.setRegion(Region, animated:true)
        initialLoc = 0
    }
    println(coord.latitude)
    println(coord.longitude)
  }

  // Location Manager Delegate stuff
  // If failed
  func locationManager(manager: CLLocationManager!, didFailWithError error: NSError!) {
    locationManager.stopUpdatingLocation()
    if (error) {
        if (seenError == false) {
            seenError = true
            print(error)
        }
     }
  }

  override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
  }
}

最佳答案

如果我理解正确,我认为最好的选择是使其成为实例变量,正如您所说。这可以通过在类顶部的其他实例变量在函数外部声明它来完成(星号用于向您显示我添加的内容):

 class ViewController: UIViewController, CLLocationManagerDelegate, MKMapViewDelegate {
    
    var locationManager: CLLocationManager!
    var seenError : Bool = false
    var theSpan: MKCoordinateSpan = MKCoordinateSpanMake(0.1, 0.1)
    var locationStatus : NSString = "Not Started"
    
    var initialLoc:Int = 1

// Declare coordinate variable
 ***var coord: CLLocationCoordinate2D?***

问号将变量声明为 optional ,因此您不必立即为其赋值。
然后,您将 locationObj.coordinate 值分配给 locationManager 函数中的 coord,但是由于您已经声明了 函数外部的 coord 变量作为实例变量,您可以删除 var coord = locationObj.coordinate 中的 var :

    func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) {
    
        var locationArray = locations as NSArray
        var locationObj = locationArray.lastObject as CLLocation

        //Assign value to coord variable
     ***coord = locationObj.coordinate***

        if initialLoc == 1 {
            var Region:MKCoordinateRegion = MKCoordinateRegionMake(coord, theSpan)
            self.Map.setRegion(Region, animated:true)
            initialLoc = 0
        }

然后,除了类中的任何其他函数(如全局变量)之外,您还可以在函数的其余部分中正常使用该变量。
祝你好运!
附:了解如何做好这一点,因为这是处理函数和变量时一直使用的方法

关于function - 快速访问函数外部的变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25235386/

相关文章:

从c代码调用freebsd中的内核变量

c - 如何在C中更新外部变量

c++ - 如何在单个全局数组中独立注册全局变量

c# - 如何接受函数引用作为参数?

c++ - 从没有参数的函数返回整数数组

swift - 在 UIScrollView 中绘制 UIImage

IOS处理打开的网址 swift

javascript - Onkeydown JS函数添加调用相同函数的输入

c++ - 使用C++中的模板从函数返回指针

ios - Swift:多项 for 循环检查数组中的输入值