我遵循了正确使用CoreBluetooth的所有步骤,但是Xcode一直告诉我以下内容:


  Objective-C方法
  'centralManager:didDiscoverPeripheral:advertisementData:RSSI:'
  方法提供
  'centralManager(:didDiscoverPeripheral:advertisementData:RSSI :)'
  与可选需求方法冲突
  的'centralManager(:didDiscoverPeripheral:advertisementData:RSSI :)'
  协议'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.
}

}

最佳答案

参数从隐式展开的可选参数(例如CBCentralManager!)更改为普通的非可选参数(例如CBCentralManager-否!)。

函数签名应如下所示(Xcode 7.1,Swift 2.1)-

func centralManager(central: CBCentralManager, didDiscoverPeripheral peripheral: CBPeripheral, advertisementData: [String : AnyObject], RSSI: NSNumber) {

10-07 23:40