我想弃权从设备坐标,但应用程序崩溃。 任何人都知道到底发生了什么? 我想,当问权限,但该请求被拒绝,也许创造一个if语句它工作时发生错误,但我无法找到有关的任何信息。
输出: fatal error: unexpectedly found nil while unwrapping an Optional value
import UIKit
import Foundation
import CoreLocation
class ViewController: UIViewController, CLLocationManagerDelegate {
let locationManager = CLLocationManager()
override func viewDidLoad() {
super.viewDidLoad()
println("\(locationManager.location.coordinate.latitude), \(locationManager.location.coordinate.longitude)")
self.locationManager.requestAlwaysAuthorization() // Ask for Authorisation from the User.
// For use in foreground
self.locationManager.requestWhenInUseAuthorization()
if (CLLocationManager.locationServicesEnabled())
{
self.locationManager.delegate = self
self.locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters
self.locationManager.startUpdatingLocation()
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) {
var locValue:CLLocationCoordinate2D = manager.location.coordinate
var latitudeactual:Double = 0
var longitudeactual:Double = 0
latitudeactual = locValue.latitude
longitudeactual = locValue.longitude
locationManager.stopUpdatingLocation()
if latitudeactual != 0 || longitudeactual != 0 {
latitudeactual = locValue.latitude
longitudeactual = locValue.longitude
}
}
}