ios 在Swift中将CBUID转换为String

pftdvrlh  于 2023-06-25  发布在  iOS
关注(0)|答案(3)|浏览(160)

我正在扫描蓝牙设备。我想将它们记录在字符串数组中。我希望能够保存它们,但得到以下错误:

Could not cast value of type 'CBUUID' (0x1f2760918) to 'NSString' (0x1f26a42d0).

CoreBT[9728:3053605] Could not cast value of type 'CBUUID' (0x1f2760918) to '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
        
    }
}

有什么想法可以把它们作为字符串存储在数组中吗?

d7v8vwbk

d7v8vwbk1#

那条线

let uniqueID = (advertisementData["kCBAdvDataServiceUUIDs"] as! NSArray).firstObject! as! String

会产生崩溃,因为UUID不是String类型,而是完全不相关的CBUUID类型。您可以像这样从CBUUID中提取UUID字符串

guard let uuids = advertisementData["kCBAdvDataServiceUUIDs"] as? [CBUUID] else { return }
guard let uniqueID = uuids.first?.uuidString else { return }

另外,给予强制施法运算符-as!。这是一个非常糟糕的做法。使用guard语句或可选链接

41ik7eoe

41ik7eoe2#

Apple提供了一个获取字符串的API:
https://developer.apple.com/documentation/corebluetooth/cbuuid/1518742-uuidstring

var uuidString: String { get }
lo8azlld

lo8azlld3#

问题是服务UUID的类型为CBUUID,因此您应该强制转换为[CBUUID]而不是NSArray<String>。顺便说一句,在从advertisementData检索值时,使用CB...Key常量而不是字符串文字是一个更好的主意。
一旦有了一个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

    }
}

与您的问题无关,但您还应该遵守Swift命名约定,即变量名称的小写CamelCase(uidCountNumberuidCountlastUID)。

相关问题