2016-09-27 98 views
0

我一直在使用Swift創建Beacon感應應用程序的指南,但自從更新Xcode並將代碼更新爲Swift 3.0後,我收到了一個致命錯誤。Swift Beacon目前無法使用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. 
} 


} 
+0

什麼是錯誤信息? – davidgyoung

+0

致命錯誤:意外發現零,同時展開一個可選值 2016-09-27 17:27:35.045701第22項[3211:825691]致命錯誤:意外發現零,同時解包可選值 –

回答

3

的問題是,你的UUID是在錯誤的格式,所以這條線不能分析它,給變量uuid分配零:

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

該程序然後將崩潰使用uuid as! UUID操作,因爲!如果有一個零值,將會崩潰。

要解決此問題,您需要在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") 
} 

運行時檢查代碼是否沒有關閉「Invalid UUID format」路徑。

+0

謝謝!這已經解決了開始掃描資金問題,但現在locationManager基金存在問題,但我會看到我可以自己做什麼。再次感謝 –