Swift Beacon不';现在使用Swift 3.0无法工作
我一直遵循使用Swift创建信标接近应用程序的指南,但自从更新Xcode并将代码更新为Swift 3.0后,我遇到了一个致命错误 浏览这些函数时,我认为startScanning函数有问题,当它启动时,我会收到致命的错误消息 如果您能提供任何有帮助的提示,我们将不胜感激:Swift Beacon不';现在使用Swift 3.0无法工作,swift,swift3,ibeacon,beacon,Swift,Swift3,Ibeacon,Beacon,我一直遵循使用Swift创建信标接近应用程序的指南,但自从更新Xcode并将代码更新为Swift 3.0后,我遇到了一个致命错误 浏览这些函数时,我认为startScanning函数有问题,当它启动时,我会收到致命的错误消息 如果您能提供任何有帮助的提示,我们将不胜感激: import UIKit import CoreLocation class ViewController: UIViewController, CLLocationManagerDelegate { @IBOutlet w
import UIKit
import CoreLocation
class ViewController: UIViewController, CLLocationManagerDelegate {
@IBOutlet weak var distanceLabel: UILabel!
var locationManager: CLLocationManager!
override func viewDidLoad() {
super.viewDidLoad()
locationManager = CLLocationManager()
locationManager.delegate = self
locationManager.requestAlwaysAuthorization()
view.backgroundColor = UIColor.gray
print("did load")
}
func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
if status == CLAuthorizationStatus.authorizedAlways{
print("status authorized")
if CLLocationManager.isMonitoringAvailable(for: CLBeaconRegion.self){
print("is monitoring")
if CLLocationManager.isRangingAvailable() {
print("scanning")
startScanning()
}
}
}
}
func startScanning() {
print("start Scanning")
let uuid = NSUUID(uuidString: "695e5f08824c785cadc72e1dde23be04")
let beaconRegion = CLBeaconRegion(proximityUUID: uuid as! UUID, identifier: "MyBeacon")
locationManager.startMonitoring(for: beaconRegion)
locationManager.startRangingBeacons(in: beaconRegion)
}
func updateDistance(distance: CLProximity){
UIView.animate(withDuration: 1) { [unowned self] in
switch distance {
case .unknown:
self.view.backgroundColor = UIColor.gray
self.distanceLabel.text = "UNKNOWN"
print("distance Unknown")
case .far:
self.view.backgroundColor = UIColor.blue
self.distanceLabel.text = "FAR"
print("distance Far")
case .near:
self.view.backgroundColor = UIColor.orange
self.distanceLabel.text = "NEAR"
print("distance Near")
case .immediate:
self.view.backgroundColor = UIColor.red
self.distanceLabel.text = "BOOM!"
print("distance Immediate")
}
}
}
func locationManager(_ manager: CLLocationManager, didRangeBeacons beacons: [CLBeacon], in region: CLBeaconRegion) {
if beacons.count > 0 {
let beacon = beacons.first! as CLBeacon
updateDistance(distance: beacon.proximity)
print("found more than one beacon")
} else {
updateDistance(distance: .unknown)
print("found only one beacon")
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
问题是您的UUID格式错误,因此此行无法解析它,将nil分配给变量
UUID
:
let uuid = NSUUID(uuidString: "695e5f08824c785cadc72e1dde23be04")
然后,程序将使用uuid作为崩溃!UUID
操作,因为!如果存在nil值,则将崩溃
要解决此问题,需要在UUID字符串中的适当位置添加破折号。您还应该避免使用!运算符强制展开Swift中的可选变量,因为它可能导致这样的崩溃。试试这个:
if let uuid = NSUUID(uuidString: "695e5f08-824c-785c-adc7-2e1dde23be04") {
let beaconRegion = CLBeaconRegion(proximityUUID: uuid, identifier: "MyBeacon")
locationManager.startMonitoring(for: beaconRegion)
locationManager.startRangingBeacons(in: beaconRegion)
}
else {
NSLog("Invalid UUID format")
}
运行时,检查代码是否没有进入“无效UUID格式”路径。问题是您的UUID格式错误,因此此行无法解析它,将nil分配给变量
UUID
:
let uuid = NSUUID(uuidString: "695e5f08824c785cadc72e1dde23be04")
然后,程序将使用uuid作为崩溃!UUID
操作,因为!如果存在nil值,则将崩溃
要解决此问题,需要在UUID字符串中的适当位置添加破折号。您还应该避免使用!运算符强制展开Swift中的可选变量,因为它可能导致这样的崩溃。试试这个:
if let uuid = NSUUID(uuidString: "695e5f08-824c-785c-adc7-2e1dde23be04") {
let beaconRegion = CLBeaconRegion(proximityUUID: uuid, identifier: "MyBeacon")
locationManager.startMonitoring(for: beaconRegion)
locationManager.startRangingBeacons(in: beaconRegion)
}
else {
NSLog("Invalid UUID format")
}
运行时,检查代码是否未进入“无效UUID格式”路径。错误消息是什么?致命错误:在展开可选值2016-09-27 17:27:35.045701 Prject 22[3211:825691]时意外发现零致命错误:在展开可选值时意外发现零错误消息是什么?致命错误:在展开可选值时意外发现零2016-09-27 17:27:35.045701项目22[3211:825691]致命错误:在展开可选值时意外发现零谢谢!这解决了startScanning基金的问题,但现在locationManager基金出现了问题,但我会看看自己能做些什么。再次感谢谢谢你!这解决了startScanning基金的问题,但现在locationManager基金出现了问题,但我会看看自己能做些什么。再次感谢