我遵循了正确使用CoreBluetooth的所有步骤,但Xcode一直告诉我以下内容:
Objective-C方法 ' centralManager:didDiscoverPeripheral:advertisementData:RSSI:' 方法提供 ' centralManager(:didDiscoverPeripheral:advertisementData:RSSI:)' 与可选的需求方法冲突 ' centralManager(:didDiscoverPeripheral:advertisementData:RSSI:)'在 protocol' CBCentralManagerDelegate'
import UIKit
import CoreBluetooth
class SecondViewController: UIViewController, CBCentralManagerDelegate {
var centralManager:CBCentralManager!
var blueToothReady = false
override func viewDidLoad() {
super.viewDidLoad()
startUpCentralManager()
}
func startUpCentralManager() {
print("Initializing central manager")
centralManager = CBCentralManager(delegate: self, queue: nil)
}
func discoverDevices() {
print("discovering devices")
centralManager.scanForPeripheralsWithServices(nil, options: nil)
}
func centralManager(central: CBCentralManager!, didDiscoverPeripheral peripheral: CBPeripheral!, advertisementData: [NSObject : AnyObject]!, RSSI: NSNumber!) {
print("Discovered \(peripheral.name)")
}
func centralManagerDidUpdateState(central: CBCentralManager) {
print("checking state")
switch (central.state) {
case .PoweredOff:
print("CoreBluetooth BLE hardware is powered off")
case .PoweredOn:
print("CoreBluetooth BLE hardware is powered on and ready")
blueToothReady = true;
case .Resetting:
print("CoreBluetooth BLE hardware is resetting")
case .Unauthorized:
print("CoreBluetooth BLE state is unauthorized")
case .Unknown:
print("CoreBluetooth BLE state is unknown");
case .Unsupported:
print("CoreBluetooth BLE hardware is unsupported on this platform");
}
if blueToothReady {
discoverDevices()
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
答案 0 :(得分:1)
参数从隐式展开的选项(例如,CBCentralManager!)更改为普通的非选项(例如,CBCentralManager - 没有!)。
函数签名应如下所示(Xcode 7.1,Swift 2.1) -
func centralManager(central: CBCentralManager, didDiscoverPeripheral peripheral: CBPeripheral, advertisementData: [String : AnyObject], RSSI: NSNumber) {