
[英]swift fatal error: unexpectedly found nil while unwrapping an Optional value
[英]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
}
日志:致命错误:在展开Optional值时意外发现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)
}
}
Acces控制可能尚未完成。 您可以检查autorisation是否可用。
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")
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.