2015-05-30 69 views
0

我正在使用SWIFT在iOS中使用iBeacon。當我嘗試創建一個帶有字符串的NSUUID時。 XCode給出了一個奇怪的錯誤無法使用字符串創建NSUUID

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

你不能使用一個全局類變量在方法之外聲明另一個全局類變量。 –

回答

0

您不能使用一個全局類變量在方法之外聲明另一個變量。

我建議在viewDidLoad初始化regionbeaconUUIDuuidString值:

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

您可以使用此代碼

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

你不能讓你屬性默認值取決於其他財產。

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