【问题标题】:Swift Beacon doesn't work now using Swift 3.0Swift Beacon 现在无法使用 Swift 3.0
【发布时间】:2016-09-27 15:58:20
【问题描述】:

我一直遵循使用 Swift 创建 Beacon 邻近应用程序的指南,但自从更新 Xcode 并将代码更新到 Swift 3.0 后,我遇到了一个致命错误。

查看函数我认为 startScanning 函数存在问题,当它触发时我收到致命错误消息。

任何可以提供帮助的提示将不胜感激:

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.
}


}

【问题讨论】:

  • 错误信息是什么?
  • 致命错误:在展开可选值时意外发现 nil 2016-09-27 17:27:35.045701 项目 22[3211:825691] 致命错误:在展开可选值时意外发现 nil

标签: swift swift3 ibeacon beacon


【解决方案1】:

问题是你的UUID格式错误,所以这行解析失败,将nil赋给变量uuid

let uuid = NSUUID(uuidString: "695e5f08824c785cadc72e1dde23be04")

然后程序将使用uuid as! 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 格式”路径。

【讨论】:

  • 谢谢!这已经解决了startScanning资金问题,但是现在locationManager资金有问题,但我会看看我自己能做什么。再次感谢
猜你喜欢
  • 2017-04-05
  • 2017-03-08
  • 1970-01-01
  • 1970-01-01
  • 2017-03-24
  • 1970-01-01
  • 1970-01-01
  • 2017-01-26
  • 1970-01-01
相关资源
最近更新 更多