我的自定义类具有以下结构:
class BeaconFormat { }
class iBeacon:BeaconFormat{}
calss Eddystone:BeaconFormat{}
现在我想在ViewController中创建iBeacon或Eddystone的对象。
class Trilateration: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate {
var testListener:AnyObject!
var listener:AnyObject!
//Selected beacon format --> 0 = iBeacon, 1 = Eddystone
var selectedBeaconFormat:Int!
override func viewDidLoad() {
super.viewDidLoad()
self.selectedBeaconFormat = 0
if selectedBeaconFormat == 0 {
//iBeacon format
testListener = BeaconFormat.iBeacon()
}else {
//Eddystone format
testListener = BeaconFormat.Eddystone()
}
但是知道我无法访问我的子类的方法。我如何初始化子类?
更新:这是BeaconManager.swift的完整代码
class BeaconManager: NSObject {
var allSavedBeacons:[CoreDataBeacon] = [CoreDataBeacon]()
override init() {
super.init()
//Get all Beacons of room
self.allSavedBeacons = CoreDataManger.defaultListener().getBeaconObjectsByMajorID(2) as! [CoreDataBeacon]
}
/*
iBeacon
*/
class iBeacon: BeaconManager, CLLocationManagerDelegate {
//iBeacon
var iBeaconManager: CLLocationManager!
var beaconRegion:CLBeaconRegion!
let uuidString = "f7826da6-4fa2-4e98-8024-bc5b71e0893e"
let beaconIdentifier = "testEnvironment"
var beaconUUID:NSUUID!
override init() {
super.init()
beaconUUID = NSUUID(UUIDString: "f7826da6-4fa2-4e98-8024-bc5b71e0893e")!
beaconRegion = CLBeaconRegion(proximityUUID: beaconUUID,
identifier: beaconIdentifier)
iBeaconManager = CLLocationManager()
if(iBeaconManager!.respondsToSelector("requestAlwaysAuthorization")) {
iBeaconManager!.requestAlwaysAuthorization()
}
iBeaconManager.delegate = self
}
func startScanning() {
self.iBeaconManager.startRangingBeaconsInRegion(beaconRegion)
}
func stopScanning() {
self.iBeaconManager.stopRangingBeaconsInRegion(beaconRegion)
}
// Location Manager Delegate
func locationManager(manager: CLLocationManager, didRangeBeacons beacons: [CLBeacon], inRegion region: CLBeaconRegion) {
print(beacons)
}
}
/*
EDDYSTONE
*/
class Eddystone: BeaconManager, BeaconScannerDelegate {
//Eddystone
var beaconScannerEddystone: BeaconScanner!
override init() {
super.init()
beaconScannerEddystone = BeaconScanner()
beaconScannerEddystone.delegate = self
}
func startScanning() {
beaconScannerEddystone.startScanning()
}
func stopScanning() {
beaconScannerEddystone.stopScanning()
}
//EddyStone Delegate Methods
func didFindBeacon(beaconScanner: BeaconScanner, beaconInfo: BeaconInfo) {
}
func didLoseBeacon(beaconScanner: BeaconScanner, beaconInfo: BeaconInfo) {
}
func didUpdateBeacon(beaconScanner: BeaconScanner, beaconInfo: BeaconInfo) {
//TODO: Something for Calibration and Positioning
}
}