ios - didDiscoverPeripheral "fail to build"错误

标签 ios swift core-bluetooth

我不确定为什么这段代码无法构建并且错误消息看起来很神秘。

代码:

var centralManager: CBCentralManager!;
var nrf8001Peripheral: CBPeripheral!;

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

    // initialize centralManager
    self.centralManager = CBCentralManager(delegate: self, queue: nil);

    // start scanning for device
    self.centralManager.scanForPeripheralsWithServices([UART_SERVICE_UUID], options:nil);
}

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

            //print out the name of the scanned peripheral
            print("Discovered \(peripheral.name)")

            //print out the UUID of the scanned peripheral
            print("NSUUID string \(peripheral.identifier.UUIDString)")

            //stop scanning when found
            self.centralManager.stopScan()

            //connect when found
            self.centralManager.connectPeripheral(peripheral, options:nil);
}

我从 XCode 编译器收到的错误是:

“由方法“centralManager(:didDiscoverPeripheral:advertisementData:RSSI:)”提供的 Objective-C 方法“centralManager:didDiscoverPeripheral:advertisementData:RSSI:”与可选的要求方法“centralManager(:didDiscoverPeripheral) 冲突:advertisementData:RSSI :)' 在协议(protocol) 'CBCentralManagerDelegate' 中”

通过查看 CoreBluetooth 文档,似乎方法语法和参数是正确的,并且参数的可选性直接从规范表中复制:https://developer.apple.com/library/ios/documentation/CoreBluetooth/Reference/CBCentralManagerDelegate_Protocol/#//apple_ref/occ/intfm/CBCentralManagerDelegate/centralManager:didDiscoverPeripheral:advertisementData:RSSI :

任何帮助将不胜感激!谢谢

根据评论:

  1. 使用 XCode 7 测试版
  2. 当我将函数声明更改为:

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

我仍然遇到相同的构建错误。

  1. 我的 centralManagerDidUpdateState: 方法是

    func centralManagerDidUpdateState(central: CBCentralManager) {
    
    print("centralManagerDidUpdateState:");
    
    switch (central.state) {
    
        case .PoweredOff:
            print("CBCentralManagerStatePoweredOff");
    
        case .Resetting:
            print("CBCentralManagerStateResetting");
    
        case .PoweredOn:
            print("CBCentralManagerStatePoweredOn");
    
        //scan for peripheral devices
        self.centralManager.scanForPeripheralsWithServices([UART_SERVICE_UUID], options:nil);
    
        case .Unauthorized:
            print("CBCentralManagerStateUnauthorized");
    
        case .Unsupported:
            print("CBCentralManagerStateUnsupported");
    
        default:
            print("CBCentralManagerStateUnknown");
        }
    }
    

最佳答案

感谢您的建议;我最终通过 XCode 7 文档找到了答案。以下函数的 XCode 6 语法如下:

func centralManagerDidUpdateState(central: CBCentralManager!) {}

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

func centralManager(central: CBCentralManager!, didConnectPeripheral peripheral: CBPeripheral!) {}

func centralManager(central: CBCentralManager!, didDisconnectPeripheral peripheral: CBPeripheral!, error: NSError!) {}

func peripheral(peripheral: CBPeripheral!, didDiscoverServices error: NSError!) {}

func peripheral(peripheral: CBPeripheral!, didDiscoverCharacteristicsForService service: CBService!, error: NSError!) {}

func peripheral(peripheral: CBPeripheral!, didUpdateNotificationStateForCharacteristic characteristic: CBCharacteristic!, error: NSError!) {}

func peripheral(peripheral: CBPeripheral!, didUpdateValueForCharacteristic characteristic: CBCharacteristic!, error: NSError!) {}

但是,这些函数将与 XCode 7 CoreBluetooth 库声明冲突。

注意可选值数据类型的不同用法。

(XCode 6) 错误:NSError! 对比 (XCode 7) 错误: NSError?

(XCode 6) advertisementData : [NSObject : AnyObject]! vs. (XCode 7) advertisementData [String : AnyObject]

适用于 XCode 7 beta 的函数声明实际上如下:

func centralManagerDidUpdateState(central: CBCentralManager) {}

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

func centralManager(central: CBCentralManager, didConnectPeripheral peripheral: CBPeripheral) {}

func centralManager(central: CBCentralManager, didFailToConnectPeripheral peripheral: CBPeripheral, error: NSError?) {}

func peripheral(peripheral: CBPeripheral, didDiscoverServices error: NSError?) {}

func peripheral(peripheral: CBPeripheral, didDiscoverCharacteristicsForService service: CBService, error: NSError?) {}

func peripheral(peripheral: CBPeripheral, didUpdateValueForCharacteristic characteristic: CBCharacteristic, error: NSError?) {}

func peripheral(peripheral: CBPeripheral, didUpdateNotificationStateForCharacteristic characteristic: CBCharacteristic, error: NSError?) {}

func centralManager(central: CBCentralManager, didDisconnectPeripheral peripheral: CBPeripheral, error: NSError?) {}

希望对遇到同样问题的其他人有所帮助!

关于ios - didDiscoverPeripheral "fail to build"错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31576864/

相关文章:

iphone - Xcode 中的条件编译

swift - macOS 远程推送通知不显示警报横幅

ios - iOS 8 中没有出现 UITableViewCell 约束。我该如何缓解这种情况?

ios - 如何在 Objective C 中将 CBCharacteristic.value 转换为 NSString

iphone - 帮助在 TableView 中插入一个新部分

iOS - 当 View Controller 以模态方式呈现时,如何将 View 保持在前面?

ios - 解析今日扩展 "Mach-O Linked Error"

ios - CoreBluetooth 设备名称更改

ios - 核心蓝牙的 didUpdateValueFor 回调函数不触发

ios - 搜索自定义表格 View 单元格不起作用并返回 nil