在swift中访问函数外部的变量

时间:2014-08-11 03:10:13

标签: 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.
  }
}

2 个答案:

答案 0 :(得分:2)

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

 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值分配给coord函数中的locationManager,但是因为您已将函数外的coord变量声明为实例变量,所以可以删除var中的var coord = locationObj.coordinate

    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
        }

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

答案 1 :(得分:0)

在您将位置读入局部变量的位置,而是将其读入实例变量。所以而不是:

var locationObj = locationArray.lastObject as CLLocation

使用

self.locationObj = locationArray.lastObject as CLLocation

并声明

var locationObj : CLLocation?

ViewController中。然后,您可以使用'dot'表示法访问locationObj