CLLocationManager fatal error: unexpectedly found nil while unwrapping an Optional value Swift
CLLocationManager fatal error: unexpectedly found nil while unwrapping an Optional value Swift
我有一个问题,因为当我尝试获取位置用户并在我的 .plist 中获得我的代码权限时,我的应用程序崩溃了。
import UIKit
import MapKit
import CoreLocation
class mapClass : UIViewController,MKMapViewDelegate,CLLocationManagerDelegate{
var location4 = CLLocation()
var lm = CLLocationManager ()
@IBOutlet var propMapa: MKMapView!
lm.delegate=self
lm.desiredAccuracy = kCLLocationAccuracyNearestTenMeters
lm.distanceFilter = kCLDistanceFilterNone
lm.startUpdatingLocation()
println("\(lm.location)") <---- nil
location4 = lm.location <---- crash
}
日志:致命错误:在展开可选值时意外发现 nil
调用 lm.startUpdatingLocation()
后,位置管理器会在检测到位置更改时调用 didUpdateLocations()
,因此您需要实施 CLLocationManager::didUpdateLocations
来检索位置:
import UIKit
import MapKit
import CoreLocation
class mapClass : UIViewController,MKMapViewDelegate,CLLocationManagerDelegate{
var location4 = CLLocation()
var lm = CLLocationManager ()
@IBOutlet var propMapa: MKMapView!
...
lm.delegate=self
lm.desiredAccuracy = kCLLocationAccuracyNearestTenMeters
lm.distanceFilter = kCLDistanceFilterNone
lm.startUpdatingLocation()
}
func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) {
let currentLocation = locations.last as CLLocation
println(currentLocation)
...
}
import UIKit
import MapKit
import CoreLocation
class ViewController: UIViewController,MKMapViewDelegate,CLLocationManagerDelegate
{
@IBOutlet weak var mapView: MKMapView!
@IBOutlet weak var textField: UITextField!
@IBOutlet weak var searchButton: UIButton!
let locationManager = CLLocationManager()
var location:CLLocationCoordinate2D!
override func viewDidLoad() {
super.viewDidLoad()
println("Search Button Clicked");
mapView.showsUserLocation = true;
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func searchButtonClicked(sender:AnyObject)
{
locationManager.distanceFilter = kCLDistanceFilterNone
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.delegate = self
locationManager.requestWhenInUseAuthorization()
locationManager.startUpdatingLocation()
}
func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) {
println("delegate called")
if manager.location != nil
{
println(locationManager.location.coordinate.latitude)
let artwork = Artwork(title: "Ram",
locationName: "Infotech",
discipline: "Testing",
coordinate: CLLocationCoordinate2D(latitude:manager.location.coordinate.latitude, longitude:manager.location.coordinate.longitude))
mapView.addAnnotation(artwork)
}
}
func locationManager(manager: CLLocationManager!, didFailWithError error: NSError!)
{
println(error)
}
}
访问控制可能尚未完成。您可以查看授权是否可用。
func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
if CLLocationManager.locationServicesEnabled() {
switch CLLocationManager.authorizationStatus() {
case .notDetermined, .restricted, .denied:
print("No access")
case .authorizedAlways, .authorizedWhenInUse:
let sourceCoordinate = locationManager.location?.coordinate
showRouteOnMap(pickupCoordinate: sourceCoordinate!, destinationCoordinate: CLLocationCoordinate2DMake(41.037141,28.9834662))
}
} else {
print("Location services are not enabled")
}
}
我有一个问题,因为当我尝试获取位置用户并在我的 .plist 中获得我的代码权限时,我的应用程序崩溃了。
import UIKit
import MapKit
import CoreLocation
class mapClass : UIViewController,MKMapViewDelegate,CLLocationManagerDelegate{
var location4 = CLLocation()
var lm = CLLocationManager ()
@IBOutlet var propMapa: MKMapView!
lm.delegate=self
lm.desiredAccuracy = kCLLocationAccuracyNearestTenMeters
lm.distanceFilter = kCLDistanceFilterNone
lm.startUpdatingLocation()
println("\(lm.location)") <---- nil
location4 = lm.location <---- crash
}
日志:致命错误:在展开可选值时意外发现 nil
调用 lm.startUpdatingLocation()
后,位置管理器会在检测到位置更改时调用 didUpdateLocations()
,因此您需要实施 CLLocationManager::didUpdateLocations
来检索位置:
import UIKit
import MapKit
import CoreLocation
class mapClass : UIViewController,MKMapViewDelegate,CLLocationManagerDelegate{
var location4 = CLLocation()
var lm = CLLocationManager ()
@IBOutlet var propMapa: MKMapView!
...
lm.delegate=self
lm.desiredAccuracy = kCLLocationAccuracyNearestTenMeters
lm.distanceFilter = kCLDistanceFilterNone
lm.startUpdatingLocation()
}
func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) {
let currentLocation = locations.last as CLLocation
println(currentLocation)
...
}
import UIKit
import MapKit
import CoreLocation
class ViewController: UIViewController,MKMapViewDelegate,CLLocationManagerDelegate
{
@IBOutlet weak var mapView: MKMapView!
@IBOutlet weak var textField: UITextField!
@IBOutlet weak var searchButton: UIButton!
let locationManager = CLLocationManager()
var location:CLLocationCoordinate2D!
override func viewDidLoad() {
super.viewDidLoad()
println("Search Button Clicked");
mapView.showsUserLocation = true;
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func searchButtonClicked(sender:AnyObject)
{
locationManager.distanceFilter = kCLDistanceFilterNone
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.delegate = self
locationManager.requestWhenInUseAuthorization()
locationManager.startUpdatingLocation()
}
func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) {
println("delegate called")
if manager.location != nil
{
println(locationManager.location.coordinate.latitude)
let artwork = Artwork(title: "Ram",
locationName: "Infotech",
discipline: "Testing",
coordinate: CLLocationCoordinate2D(latitude:manager.location.coordinate.latitude, longitude:manager.location.coordinate.longitude))
mapView.addAnnotation(artwork)
}
}
func locationManager(manager: CLLocationManager!, didFailWithError error: NSError!)
{
println(error)
}
}
访问控制可能尚未完成。您可以查看授权是否可用。
func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
if CLLocationManager.locationServicesEnabled() {
switch CLLocationManager.authorizationStatus() {
case .notDetermined, .restricted, .denied:
print("No access")
case .authorizedAlways, .authorizedWhenInUse:
let sourceCoordinate = locationManager.location?.coordinate
showRouteOnMap(pickupCoordinate: sourceCoordinate!, destinationCoordinate: CLLocationCoordinate2DMake(41.037141,28.9834662))
}
} else {
print("Location services are not enabled")
}
}