gpt4 book ai didi

ios - 使用 Swift 在 CoreLocation 中搜索区域的正确方法是什么?

转载 作者:行者123 更新时间:2023-11-30 14:18:37 25 4
gpt4 key购买 nike

我一直在研究许多教程,并且每个教程都不断遇到完全相同的问题。当我在其他教程中运行以下代码或它的其他变体时,我不断收到:

2015-06-12 13:50:33.797 Scanner[4599:2593734] *** Assertion failure in -[CLLocationManager startRangingBeaconsInRegion:], /SourceCache/CoreLocationFramework/CoreLocation-1756.0.20/Framework/CoreLocation/CLLocationManager.m:1129
2015-06-12 13:50:33.798 Scanner[4599:2593734] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid parameter not satisfying: region != nil'
*** First throw call stack:
(0x183efc2d8 0x1957200e4 0x183efc198 0x184db0ed4 0x18464f0d4 0x10008bebc 0x10008bf60 0x18893cc84 0x18893c994 0x1889431d0 0x188940880 0x1889b28ec 0x188bc6a94 0x188bc9208 0x188bc7778 0x18c7053c8 0x183eb427c 0x183eb3384 0x183eb19a8 0x183ddd2d4 0x1889a843c 0x1889a2fac 0x1000923b4 0x195d9ea08)
libc++abi.dylib: terminating with uncaught exception of type NSException

我将 (region) 切换到 (region!) 并得到一个输出,表明解包意外的 nil 时发生错误。

我非常恼火,并尝试了多种可能性。有谁知道问题是什么?当屏幕加载时它崩溃了。

import UIKit
import CoreLocation


class ViewController: UIViewController, CLLocationManagerDelegate {
let locationManager = CLLocationManager()
let region = CLBeaconRegion(proximityUUID: NSUUID(UUIDString: "1800"), identifier: "BLE113")

override func viewDidLoad(){
super.viewDidLoad()

locationManager.delegate = self;

if (CLLocationManager.authorizationStatus() != CLAuthorizationStatus.AuthorizedWhenInUse) {
locationManager.requestWhenInUseAuthorization()
}

locationManager.startRangingBeaconsInRegion(region)
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}

func locationManager(manager: CLLocationManager!, didRangeBeacons beacons: [AnyObject]!, inRegion region: CLBeaconRegion!) {
println(beacons)
}

}

最佳答案

这是我的代码版本,具有正确的签名。请注意,NSUUID:UUIDString 是一个可选的初始值设定项,可以返回 nil。

导入UIKit导入核心位置

ViewController 类:UIViewController、CLLocationManagerDelegate {

let locationManager = CLLocationManager()
var region: CLBeaconRegion?
override func viewDidLoad(){
if let proximityUUID = NSUUID(UUIDString: "40ACF125-D06F-492D-BD4F-0FC9D93F906C") { // generated using uuidgen tool

self.region = CLBeaconRegion(proximityUUID: proximityUUID, identifier: "BLE113")
if let region = self.region {
locationManager.delegate = self

if (CLLocationManager.authorizationStatus() != CLAuthorizationStatus.AuthorizedWhenInUse) {
locationManager.requestWhenInUseAuthorization()
}

locationManager.startRangingBeaconsInRegion(region)
}
}
super.viewDidLoad()
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}


func locationManager(manager: CLLocationManager, didRangeBeacons beacons: [CLBeacon], inRegion region: CLBeaconRegion) {
print(beacons)
}

}

运行它,看看哪里出了问题?你所在的地区是零吗?因为如果您的 NSUUID 格式不正确,CLBeaconRegion 将返回 nil。

关于ios - 使用 Swift 在 CoreLocation 中搜索区域的正确方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30812025/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com