A1exFF
A1exFF

Reputation: 3

Can't create NSUUID with string

I'm working with iBeacon in iOS by using SWIFT. When I try to create a NSUUID with a string. XCode gives a strange error

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

But actually, I already declared the var uuidString, and give a value to it

I also try like this let uuid = NSUUID(UUIDString: ""), this can work. So anyone has idea what's going on?

Here is my code

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

Upvotes: 0

Views: 466

Answers (3)

Leo
Leo

Reputation: 24714

You can use this code

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

You cannot let you property default value depends on other property.

Upvotes: 2

BadmintonCat
BadmintonCat

Reputation: 9586

Or you could assign the two object-type properties inside 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)
    }
}

Upvotes: 0

Lyndsey Scott
Lyndsey Scott

Reputation: 37290

You can't use one global class variable to declare another outside of a method.

I'd recommend initializing region and beaconUUID with your uuidString value in viewDidLoad:

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

Upvotes: 0

Related Questions