无法使用字符串创建NSUUID

时间:2015-05-30 05:04:19

标签: ios swift

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

  

ViewController.Type没有名为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)
    }
}

3 个答案:

答案 0 :(得分:2)

您可以使用此代码

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

你不能让你的财产默认值取决于其他财产。

答案 1 :(得分:0)

您不能使用一个全局类变量来声明方法之外的另一个。

我建议使用region中的beaconUUID值初始化uuidStringviewDidLoad

let uuidString:String! = "FDA50693-A4E2-4FB1-AFCF-C6EB07647825"
var beaconUUID:NSUUID!

let locationManager = CLLocationManager()

var region:CLBeaconRegion! 

override func viewDidLoad() {

    beaconUUID = NSUUID(UUIDString: uuidString)
    region = CLBeaconRegion(proximityUUID: beaconUUID, identifier: "Beacon")

答案 2 :(得分:0)

或者您可以在viewDidLoad()...

中分配两个对象类型属性
import UIKit
import CoreLocation
import CoreBluetooth

class ViewController: UIViewController, CLLocationManagerDelegate {
    let uuidString:String = "FDA50693-A4E2-4FB1-AFCF-C6EB07647825"
    let locationManager = CLLocationManager()

    var beaconUUID:NSUUID?
    var region:CLBeaconRegion?


    override func viewDidLoad() {
        super.viewDidLoad()

        beaconUUID = NSUUID(UUIDString: uuidString)
        region = CLBeaconRegion(proximityUUID: beaconUUID, identifier: "Beacon")

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