I just started learning Swift and SwiftUI and I came across a problem that I'm having some trouble to solve.
I'm trying to create a list of available Bluetooth peripherals with buttons to select which to connect to, but for some reason the ForEach statement is iterating CBPeer objects instead of CBPeripheral ones.
Am I doing something wrong or it's just not possible to iterate through an array of CBPeripheral objects?
My code:
struct BluetoothPeripheralSelection: View{
var peripherals: [CBPeripheral]
var body: some View{
VStack{
Section(header: Text("Select bluetooth peripheral")){
ForEach(peripherals, id: \.self){peripheral in
Button(action: {}){
Text(peripheral.name)
}
}
}
}
}
}
It looks like compiler reports wrong error. The reason is that peripheral.name
is optional, so use instead something like (tested with Xcode 12)
Button(action: {}){
Text(peripheral.name ?? peripheral.identifier.uuidString)
}