didDiscoverPeripheral“无法构建”错误 [英] didDiscoverPeripheral "fail to build" error
问题描述
我不确定为什么此代码无法构建,并且错误消息似乎很隐秘。
I'm not sure why this code is failing to build and the error message seems quite cryptic.
代码:
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编译器收到的错误是:
And the error I receive from the XCode compiler is:
目标C方法'centralManager:didDiscoverPeripheral:advertisementData:RSSI:'由方法'centralManager(:didDiscoverPeripheral:advertisementData:RSSI :)'提供,与可选需求方法'centralManager冲突(:didDiscoverPeripheral:advertisementData:RSSI :)在协议'CBCentralManagerDelegate'中
"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'"
通过查看CoreBluetooth文档,似乎方法的语法和参数似乎正确,并且参数的可选性直接从规格表中复制: https://developer.apple.com/library/ios/documentation/CoreBluetooth/Reference/CBCentralManagerDelegate_Protoc ol /#// apple_ref / occ / intfm / CBCentralManagerDelegate / centralManager:didDiscoverPeripheral:advertisementData:RSSI :
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
根据评论:
- 使用XCode 7 beta
-
当我将函数声明更改为:
- Using XCode 7 beta
When I change the function declaration to:
func centralManager(central:CBCentralManager,didDiscoverPeripheral外设:CBPeripheral,adsData广告数据:[ NSObject:AnyObject],RSSI RSSI:NSNumber)
func centralManager(central: CBCentralManager, didDiscoverPeripheral peripheral: CBPeripheral, advertisementData advertisementData: [NSObject : AnyObject], RSSI RSSI: NSNumber)
我仍然遇到相同的构建错误。
I still get the same build error.
-
我的centralManagerDidUpdateState:方法是
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");
}
}
推荐答案
感谢您的建议;我最终通过XCode 7文档找到了答案。以下功能的XCode 6语法如下:
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!) {}
但是,这些函数将与XCode 7 CoreBluetooth库声明冲突。
However, these functions will conflict with the XCode 7 CoreBluetooth library declarations.
请注意可选和数据类型的不同用法。
(XCode 6) 错误:NSError!
与 (XCode 7) error:NSError?
(XCode 6) advertisementData:[NSObject:AnyObject]!
与。 (XCode 7) advertisementData [String:AnyObject]
实际上,XCode 7 beta的适当函数声明如下:
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!
这篇关于didDiscoverPeripheral“无法构建”错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!