gpt4 book ai didi

ios - 无法使用字符串创建 NSUUID

转载 作者:行者123 更新时间:2023-11-28 13:13:05 25 4
gpt4 key购买 nike

我正在使用 SWIFT 在 iOS 中使用 iBeacon。当我尝试使用字符串创建 NSUUID 时。 XCode 给出一个奇怪的错误

ViewController.Type doesn't have a member named uuidString.

但实际上,我已经声明了var uuidString,并赋值给它

我也这样尝试 let uuid = NSUUID(UUIDString: ""),这可行。所以有人知道发生了什么事吗?

这是我的代码

import UIKit
import CoreLocation
import CoreBluetooth

class ViewController: UIViewController, CLLocationManagerDelegate {
let uuidString:String! = "FDA50693-A4E2-4FB1-AFCF-C6EB07647825"
let beaconUUID:NSUUID = NSUUID(UUIDString: uuidString)!

let locationManager = CLLocationManager()

let region = CLBeaconRegion(proximityUUID: beaconUUID, identifier: "Beacon")


override func viewDidLoad() {
super.viewDidLoad()

locationManager.delegate = self
// Do any additional setup after loading the view, typically from a nib.

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

//locationManager.startRangingBeaconsInRegion(region)
}

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

最佳答案

您可以使用此代码

import UIKit
import CoreLocation
import CoreBluetooth

class ViewController: UIViewController, CLLocationManagerDelegate {


let locationManager = CLLocationManager()

let region:CLBeaconRegion = {
let uuidString:String! = "FDA50693-A4E2-4FB1-AFCF-C6EB07647825"
let beaconUUID:NSUUID = NSUUID(UUIDString: uuidString)!
let result = CLBeaconRegion(proximityUUID: beaconUUID, identifier: "Beacon")
return result
}()



override func viewDidLoad() {
super.viewDidLoad()

locationManager.delegate = self
// Do any additional setup after loading the view, typically from a nib.

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

//locationManager.startRangingBeaconsInRegion(region)
}

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

你不能让你的属性默认值依赖于其他属性。

关于ios - 无法使用字符串创建 NSUUID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30542463/

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