我正在使用SWIFT在iBeacon中使用iOS。当我尝试用字符串创建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)
}
}发布于 2015-05-30 05:14:48
不能使用一个全局类变量来声明方法之外的另一个类变量。
我建议用region和beaconUUID中的uuidString值初始化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")发布于 2015-05-30 05:08:10
您可以使用以下代码
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)
}
}不能让属性默认值取决于其他属性。
发布于 2015-05-30 05:15:28
或者您可以在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)
}
}https://stackoverflow.com/questions/30542463
复制相似问题