gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 01:43:57 25 4
gpt4 key购买 nike

我从 获取坐标,但我希望能够在位置管理器功能之外使用它们。是否可以使它们成为实例变量?如果是这样,有人可以给我一些示例代码吗?在下面的代码中,我在 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com