[英]Converting CBUUID to String in Swift
I am scanning for BlueTooth devices. 我正在扫描蓝牙设备。 I want to log them in an array of strings. 我想将它们记录在字符串数组中。 I want to be able to save them but get the following errors: 我希望能够保存它们,但出现以下错误:
Could not cast value of type 'CBUUID' (0x1f2760918) to 'NSString' (0x1f26a42d0). 无法将类型'CBUUID'(0x1f2760918)的值强制转换为'NSString'(0x1f26a42d0)。
CoreBT[9728:3053605] Could not cast value of type 'CBUUID' (0x1f2760918) to 'NSString' (0x1f26a42d0). CoreBT [9728:3053605]无法将类型'CBUUID'(0x1f2760918)的值强制转换为'NSString'(0x1f26a42d0)。
I have the following code below. 我下面有以下代码。 There is no reference to an array as I can't even get it to a string. 没有对数组的引用,因为我什至无法将其获取为字符串。
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
}
}
Any thoughts on getting these as strings so I can store them in an array. 关于将它们作为字符串获取的任何想法,这样我就可以将它们存储在数组中。 If you have a better way please let me know. 如果您有更好的方法,请告诉我。 Thank you! 谢谢!
The line 线
let uniqueID = (advertisementData["kCBAdvDataServiceUUIDs"] as! NSArray).firstObject! as! String
produces a crash because the UUID is not of String
type but of completely unrelated CBUUID
type. 因为UUID不是String
类型而是完全不相关的CBUUID
类型,所以会导致崩溃。 You can extract UUID string from CBUUID
like so 您可以像这样从CBUUID
提取UUID字符串
guard let uuids = advertisementData["kCBAdvDataServiceUUIDs"] as? [CBUUID] else { return }
guard let uniqueID = uuids.first?.uuidString else { return }
Also, give up force cast operator - as!
另外,放弃强制施放运算符as!
. 。 This is a really bad practice. 这是一个非常糟糕的做法。 Use guard
statements or optional chaining 使用guard
声明或可选链接
The issue is that the Service UUIDs are of type CBUUID
, so you should cast to [CBUUID]
instead of NSArray<String>
. 问题是服务UUID的类型为CBUUID
,因此您应[CBUUID]
为[CBUUID]
而不是NSArray<String>
。 Btw it's a better idea to use the CB...Key
constants instead of String literals when retrieving values from advertisementData
. 顺便说一下这是一个更好的主意,使用CB...Key
常量,而不是字符串字面从检索值时, advertisementData
。
Once you have a single CBUUID
instance, you can use uuidString
to convert it to a String
and display it on a UILabel
. 拥有单个CBUUID
实例后,可以使用uuidString
将其转换为String
并将其显示在UILabel
。
func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String : Any], rssi RSSI: NSNumber) {
if let peripheralName = advertisementData[CBAdvertisementDataLocalNameKey] as? String {
print("peripheral Name: \(peripheralName)")
guard let uniqueIDs = advertisementData[CBAdvertisementDataServiceUUIDsKey] as? [CBUUID], let uniqueID = uniqueIDs.first else { return }
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.uuidString
}
}
Unrelated to your issue, but you should also conform to the Swift naming convention, which is lowerCamelCase for variable names ( uidCountNumber
, uidCount
and lastUID
). 与您的问题无关,但您还应遵循Swift命名约定,即变量名称( uidCountNumber
, uidCount
和lastUID
)的lowerCamelCase。
Apple provides an API for getting the string: Apple提供了用于获取字符串的API:
https://developer.apple.com/documentation/corebluetooth/cbuuid/1518742-uuidstring https://developer.apple.com/documentation/corebluetooth/cbuuid/1518742-uuidstring
var uuidString: String { get }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.