快速获得坐标时为零 [英] Nil when obtain coordinates in swift

查看:121
本文介绍了快速获得坐标时为零的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从设备中弃用坐标,但是应用程序崩溃了.有人知道会发生什么吗? 我想错误是在请求许可但发生了错误,但是请求被拒绝了,也许创建了一个if语句可以正常工作,但是我找不到关于它的任何信息.

I am trying to abstain coordinates from the device, but the app crash. Anyone know what might have happened? I suppose the error happened when it asked for permission but the request was denied, maybe creating an if statement it works, but I couldn't find any information about that.

输出: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

        }

}
}

推荐答案

location属性是一个隐式展开的CLLocation可选,它可能是nil.除非您知道它不是nil,否则永远不要访问隐式展开的可选成员.您可以使用以下命令确认它是nil:

The location property is an implicitly unwrapped CLLocation optional, and it is likely nil. You should never access members of implicitly unwrapped optional unless you know it is not nil. You can confirm it is nil with the following:

if locationManager.location != nil {
    println("\(locationManager.location.coordinate.latitude), \(locationManager.location.coordinate.longitude)")
} else {
    println("locationManager.location is nil")
}

或者您可以:

println("\(locationManager.location)")

但是在未先检查其是否为nil之前,请勿尝试访问隐式展开对象的属性.

But don't try to access the properties of an implicitly unwrapped object without first checking to see if it's nil or not.

通常,除非启动了位置服务并且已经开始进行位置更新(例如,您已经看到didUpdateLocations被调用),否则您不应该期望location属性的有效CLLocation对象.确定位置的过程是异步发生的,并且预计在viewDidLoad中将不可用.应该将特定于位置的逻辑放在didUpdateLocations方法中.

As a general rule, you should not expect a valid CLLocation object for the location property unless location services were started and location updates have already started to come in (e.g. you've seen didUpdateLocations called). The process of determining location happens asynchronously and is not to be expected to be available in viewDidLoad. One should put location-specific logic inside the didUpdateLocations method.

这篇关于快速获得坐标时为零的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆