-1

For some strange reason it will crash the first time I try to run the program but if I run it again while the simulator is still running it works fine. Here is a snippit of code that is giving the issue.

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!))
}
Broque
  • 9
  • 3
  • 1
    That's an async method. You need to implement `func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation])` and check the location there. – Leo Dabus Nov 07 '18 at 03:48
  • I have included this function into my model below the init. Do you mind explaining to me a bit more what this function is capable of? – Broque Nov 07 '18 at 03:58
  • It is called when the location is received or if it changes – Leo Dabus Nov 07 '18 at 04:00
  • Note that if you just need to get the location once you an use CLLocation instance method `requestLocation()` https://developer.apple.com/documentation/corelocation/cllocationmanager/1620548-requestlocation It avoids unnecessary battery usage. – Leo Dabus Nov 07 '18 at 04:01
  • I appreciate your input but I am still having the same issue with the new method. First run is nil, second isnt – Broque Nov 07 '18 at 04:40
  • You can only check the location property inside that method. That property will always be nil inside viewDidLoad method – Leo Dabus Nov 07 '18 at 04:46

1 Answers1

0

You've to implement locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) for CLLocationManagerDelegate to work, like this

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)
  }
}
Linus Oleander
  • 17,746
  • 15
  • 69
  • 102