在swift中访问函数外部的变量 [英] Accessing Variables outside of a function in swift
问题描述
我从坐标获取坐标,但我希望能够在位置管理器功能外使用它们。是否有可能使他们的实例变量?如果是这样,有人可以给我一些示例代码吗?在下面的代码中,我在func中打印,它的工作原理,但我希望能够,例如在它外面打印,否则使用坐标。也许作为一个全球变量?我将如何编码?
Im getting coordinates from , but I'd like to be able to use them outside the Location Manager Function. Is it possible to make them instance variables? If so, could someone give me some example code for that? In the code below I print within the func and it works, but I'd like to be able to, for example print outside it, and otherwise use the coordinates. Perhaps as a global variable? How would i code that? I cant quite make it out from the documentation.
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.
}
}
推荐答案
正如你所说,我认为你是正确的,我认为最好的选择是让它成为一个实例变量。这可以通过在类的顶部用其他实例变量声明它来完成(星号用于显示我添加的内容):
If I understand you correctly, I think the best option would be to make it an instance variable, as you said. This would be done by declaring it outside of the function with your other instance variables at the top of your class (the asterisks are used to show you what I added):
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?***
问号将变量声明为可选,因此您不必立即为其分配值。
然后,您可以在 locationManager
中将
locationObj.coordinate
值分配给 coord
/ code>函数,但是由于您已经将函数外部的 coord
变量声明为实例变量,因此您可以删除 var
在 var coord = locationObj.coordinate
:
The question mark declares the variable as an optional, so you don't have to immediately assign a value to it.
Then, you assign the locationObj.coordinate
value to coord
in your locationManager
function, however since you already declared the coord
variable outside your function as an instance variable you can remove the var
in 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
}
然后你可以使用变量no除了类中的任何其他函数(如全局变量)之外,在函数的其余部分中都是如此。
祝您好运!
P.S.学习如何做到这一点,因为它是一种使用函数和变量的方法。
Then you can use the variable normally in the rest of your function, in addition to any other function in the class (like a global variable).
Best of luck!
P.S. Learn how to do this well, as it is a method used all the time when working with functions and variables
这篇关于在swift中访问函数外部的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!