【问题标题】:Using Variable outside locationManager()在 locationManager() 之外使用变量
【发布时间】:2019-05-19 11:10:48
【问题描述】:

我正在尝试使用 MVC 模式清理我的代码。我有一个文件“CoreLocationService.swift”,我想在其中获取位置。我想在“ViewController.swift”中使用该位置向用户展示。

CoreLocationService.swift

import Foundation
import CoreLocation

class CoreLocationService: CLLocationManager, CLLocationManagerDelegate {


    let locationManager = CLLocationManager()

    var latitude : Double?


    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        let GPS = locations[locations.count - 1] // Get the array of last location

        latitude = GPS.coordinate.latitude


    }
    func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
        print(error)
    }


    func GPSInitialize() {
        // Start GPS

        locationManager.delegate = self
        locationManager.requestWhenInUseAuthorization()
        locationManager.desiredAccuracy = kCLLocationAccuracyBest
        //locationManager.requestLocation() // One time request
        locationManager.startUpdatingLocation() // Continues location

    }


}

ViewController.swift

import UIKit
import CoreLocation

class ViewController: UIViewController, CLLocationManagerDelegate {

//    let location = CLLocationManager()

    let locationService = CoreLocationService() // CoreLocationService Class


    override func viewDidLoad() {
        super.viewDidLoad()


        locationService.GPSInitialize() // Start updating Location


        print(locationService.latitude) // Print Latitude

    }

}

我将纬度声明为要访问的全局变量来自 ViewController.swift 但它为空且仅打印“nil”的变量。

如果我在 locationManager 中打印“打印(纬度)”,它会打印坐标。

【问题讨论】:

    标签: ios swift cllocationmanager


    【解决方案1】:

    我认为为什么调用 print(loccationService.latitude) 时纬度为零是委托方法

    func locationManager(_ manager: CLLocationManager, didUpdateLocations 位置: [CLLocation])

    尚未更新纬度。

    您可以在 CoreLocationService 中添加一个回调,如下所示,

    // callback to be called after updating location
    var didUpdatedLocation: (() -> ())?
    

    然后在委托方法中调用这个闭包,

    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
            let GPS = locations[locations.count - 1] // Get the array of last location
    
            latitude = GPS.coordinate.latitude
    
            didUpdatedLocation?()
    }
    

    在你的 ViewController 中,像这样打印纬度,

    locationService.didUpdatedLocation = {
        print(locationService.latitude) // Print Latitude
    }
    

    希望这会有所帮助!

    【讨论】:

      【解决方案2】:

      在您的代码中,viewDidLoad() 函数将调用 CoreLocatinoService 类中的 GPSInitialize() 函数。此时当它执行下一行代码时,'latitude' 值将为零,因为调用 startUpdatingLocation() 时,可能不会立即调用 CLLocationManager 'didUpdateLocation' 的委托方法。

      作为解决方案,我建议使用另一个委托或闭包来通知视图控制器位置更新并传递最新的位置详细信息。

      希望这会有所帮助。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-10-31
        • 2018-05-27
        • 2012-12-27
        • 2018-09-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多