我正在扫描蓝牙设备。我想将它们记录在一个字符串数组中。我希望能够保存它们,但出现以下错误:
无法将类型“CBUUID”(0x1f2760918)的值转换为“NSString”(0x1f26a42d0)。
CoreBT[9728:3053605] 无法将类型“CBUUID”(0x1f2760918)的值转换为“NSString”(0x1f26a42d0)。
我在下面有以下代码。没有对数组的引用,因为我什至无法将其转换为字符串。
func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String : Any], rssi RSSI: NSNumber) {
if let peripheralName = advertisementData[CBAdvertisementDataLocalNameKey] as? String {
print("peripheral Name: \(peripheralName)")
let uniqueID = (advertisementData["kCBAdvDataServiceUUIDs"] as! NSArray).firstObject! as! String
// The above line produces an error
//Removing as! String will work prevent error but still cannot get from "any" to "string"
let x = advertisementData
print("x: \(x)")
let y = advertisementData["kCBAdvDataServiceUUIDs"]
print("y: \(y)")
print("uniqueID: \(uniqueID)")
self.UIDCountNumber = UIDCountNumber + 1
self.UID_Count.text = String(self.UIDCountNumber)//label counting devices
self.last_UID.text = uniqueID as? String //Label is not changing
}
}
关于将这些作为字符串获取以便我可以将它们存储在数组中的任何想法。如果你有更好的方法请告诉我。谢谢!
最佳答案
Apple 提供了获取字符串的 API:
https://developer.apple.com/documentation/corebluetooth/cbuuid/1518742-uuidstring
var uuidString: String { get }
关于ios - 在 Swift 中将 CBUUID 转换为字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53191695/