Swift:尝试打印CLLocationSpeed时发生异常“解开可选值时意外发现为零". [英] Swift: Exception while trying to print CLLocationSpeed "unexpectedly found nil while unwrapping an Optional value"
本文介绍了Swift:尝试打印CLLocationSpeed时发生异常“解开可选值时意外发现为零".的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试制作一个超级简单的程序,目前该程序可以从iPhone的gps打印速度.到目前为止,这是我的代码:
I am trying to make a super simple program that will as of right now print the speed from the iPhone's gps. This is my code so far:
//
// ViewController.swift
// GpsSpeed
//
// Created by Jacob Sandum on 4/18/15.
// Copyright (c) 2015 Jacob Sandum. All rights reserved.
//
import UIKit
import CoreLocation
let locationManager = CLLocationManager()
class ViewController: UIViewController, CLLocationManagerDelegate {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
locationManager.delegate = self
if CLLocationManager.authorizationStatus() == .NotDetermined {
locationManager.requestAlwaysAuthorization()
}
locationManager.desiredAccuracy=kCLLocationAccuracyBest
if CLLocationManager.locationServicesEnabled() {
locationManager.startUpdatingLocation()
}
var speed: CLLocationSpeed = CLLocationSpeed()
speed = locationManager.location.speed
println(speed);
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
当我尝试运行此代码时,出现此致命异常:unexpectedly found nil while unwrapping an Optional value
(lldb)
When I try to run this code I get this fatal exception: unexpectedly found nil while unwrapping an Optional value
(lldb)
我认为CLLocationSpeed为零,但是我不知道该如何解决.
I think CLLocationSpeed is nil, but I do not know how to fix this.
推荐答案
首先将NSLocationAlwaysUsageDescription
添加到您的info.plist文件中
First add NSLocationAlwaysUsageDescription
in to your info.plist file
那么完整的代码是
import UIKit
import CoreLocation
class ViewController: UIViewController, CLLocationManagerDelegate {
let locationManager = CLLocationManager()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
locationManager.delegate = self
if NSString(string:UIDevice.currentDevice().systemVersion).doubleValue > 8 {
locationManager.requestAlwaysAuthorization()
}
locationManager.desiredAccuracy=kCLLocationAccuracyBest
}
func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) {
var speed: CLLocationSpeed = CLLocationSpeed()
speed = locationManager.location.speed
println(speed);
}
func locationManager(manager: CLLocationManager!, didChangeAuthorizationStatus status: CLAuthorizationStatus) {
if status != CLAuthorizationStatus.Denied{
locationManager.startUpdatingLocation()
}
}
}
在真实设备
您的错误:
- 不要在
ViewDidLoad
中获得速度,它只会被调用一次.因此,在您的代码中,每次您尝试在开始更新之前获取位置. - 最好在位置更新时加快速度
- 最好在身份验证更改后开始更新
- Do not get speed in
ViewDidLoad
it will only be called once.So,in your code, every time you try to get location before it start updating. - It is better to get speed when location update
- It is better to start updating when auth changed
这篇关于Swift:尝试打印CLLocationSpeed时发生异常“解开可选值时意外发现为零".的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文