Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift CCLocation一直返回nil。使用单例_Swift_Singleton_Cllocation - Fatal编程技术网

Swift CCLocation一直返回nil。使用单例

Swift CCLocation一直返回nil。使用单例,swift,singleton,cllocation,Swift,Singleton,Cllocation,出于某种奇怪的原因,当我第一次尝试运行程序时,它会崩溃,但如果我在模拟器仍在运行时再次运行它,它会正常工作。下面是一些给出问题的代码 class CityList : NSObject, CLLocationManagerDelegate { // stored properties var cities: [City] var currentLocation: CLLocation? = nil static let sharedInstance = CityList() static le

出于某种奇怪的原因,当我第一次尝试运行程序时,它会崩溃,但如果我在模拟器仍在运行时再次运行它,它会正常工作。下面是一些给出问题的代码

class CityList : NSObject, CLLocationManagerDelegate {

// stored properties
var cities: [City]
var currentLocation: CLLocation? = nil
static let sharedInstance = CityList()
static let firstNotif = Notification.Name(rawValue: "test")
static let secondNotif = Notification.Name(rawValue: "test2")
let locationManager = CLLocationManager()

// initializers
override init () {
    cities = [
        City(name: "Medford", state: "Oregon", latitude: 42.3266667, longitude: -122.8744444),
        City(name: "Seattle", state: "Washington", latitude: 47.6063889, longitude: -122.3308333),
        City(name: "Grants, Pass", state: "Oregon", latitude: 42.4391667, longitude: -123.3272222),
        City(name: "Applegate", state: "Oregon", latitude: 42.2570662, longitude: -123.1683833),
        City(name: "San Francisco", state: "California", latitude: 37.775, longitude: -122.4183333)
    ]
    super.init()
    locationManager.delegate = self
    locationManager.requestWhenInUseAuthorization()// request user authorization
    locationManager.requestAlwaysAuthorization()
    locationManager.startUpdatingLocation()
    currentLocation = locationManager.location
    cities.append(City(name: "test", state: "test", location: currentLocation!))
}

您必须实现
locationManager(\manager:CLLocationManager,didUpdateLocations locations:[CLLocation])
以便
CLLocationManagerDelegate
工作,如下所示

import CoreLocation

public class Location: NSObject, CLLocationManagerDelegate {
  private let manager = CLLocationManager()

  override convenience public init() {
    self.init()
    manager.desiredAccuracy = kCLLocationAccuracyThreeKilometers
    manager.delegate = self
    manager.requestWhenInUseAuthorization()
    manager.requestAlwaysAuthorization()
    manager.startUpdatingLocation()
  }

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

  public func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
    guard let location = locations.first else {
      return
    }

    print(location.coordinate.longitude)
    print(location.coordinate.latitude)
  }
}

这是一个异步方法。您需要实现
func locationManager(manager:CLLocationManager,didUpdateLocations locations:[CLLocation])
并检查那里的位置。我已将此函数包含在init下面的模型中。你介意再向我解释一下这个函数的功能吗?它是在接收到位置时调用的,或者如果它发生了变化,那么如果你只需要在使用CLLocation实例方法
requestLocation()后获取位置,就可以调用它
它避免了不必要的电池使用。我感谢您的意见,但我仍然对新方法有同样的问题。第一次是零,第二次不是