2
votes

I'm not sure why this code is failing to build and the error message seems quite cryptic.

Code:

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);
}

And the error I receive from the XCode compiler is:

"Objective-C method 'centralManager:didDiscoverPeripheral:advertisementData:RSSI:' provided by method 'centralManager(:didDiscoverPeripheral:advertisementData:RSSI:)' conflicts with optional requirement method 'centralManager(:didDiscoverPeripheral:advertisementData:RSSI:)' in protocol 'CBCentralManagerDelegate'"

From looking through the CoreBluetooth documentation it seems as if the method syntax and parameters are correct, and the optionality of the parameters is copied directly from the spec sheet: https://developer.apple.com/library/ios/documentation/CoreBluetooth/Reference/CBCentralManagerDelegate_Protocol/#//apple_ref/occ/intfm/CBCentralManagerDelegate/centralManager:didDiscoverPeripheral:advertisementData:RSSI:

Any help would be appreciated! Thank you

Per the comments:

  1. Using XCode 7 beta
  2. When I change the function declaration to:

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

I still get the same build error.

  1. My centralManagerDidUpdateState:method is

    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");
        }
    }
    
1
What Xcode version are you using? If you're using 7, the parameters of this method should all be non-optional.Mick MacCallum
maybe you forget to implement centralManagerDidUpdateState: methodjinhua liao
@0x7fffffff Thanks, took out all the optionals in all the function declarations, but still failing to build.BeginnersMindTruly
@jinhualiao have the centralManagerDidUpdateState method shown above as well.BeginnersMindTruly
Do you have two method with the same signature declared in this class? Is it possible that you copy pasted it and forgot to delete the old one?Mick MacCallum

1 Answers

2
votes

Thank you for the suggestions; I ended up finding the answer through the XCode 7 documentation. The XCode 6 syntax for the following functions was as follows:

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!) {}

However, these functions will conflict with the XCode 7 CoreBluetooth library declarations.

Note the differing uses of optionals as well as data types.

(XCode 6) error:NSError! vs. (XCode 7) error:NSError?

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

The appropriate function declarations for XCode 7 beta are actually the following:

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?) {}

Hope this is helpful to others having the same issues!