Swiftui 在用ForEach迭代CBPeer外围设备阵列时,我得到了一个CBPeer

Swiftui 在用ForEach迭代CBPeer外围设备阵列时,我得到了一个CBPeer,swiftui,core-bluetooth,Swiftui,Core Bluetooth,我刚开始学习Swift和SwiftUI,我遇到了一个问题,我有一些麻烦要解决 我正在尝试创建一个可用蓝牙外围设备列表,其中包含用于选择连接的按钮,但出于某种原因,ForEach语句正在迭代CBPeer对象,而不是CBPeer对象 我是做错了什么,还是不可能遍历一系列外围对象 我的代码: struct BluetoothPeripheralSelection: View{ var peripherals: [CBPeripheral] var body: some Vie

我刚开始学习Swift和SwiftUI,我遇到了一个问题,我有一些麻烦要解决

我正在尝试创建一个可用蓝牙外围设备列表,其中包含用于选择连接的按钮,但出于某种原因,ForEach语句正在迭代CBPeer对象,而不是CBPeer对象

我是做错了什么,还是不可能遍历一系列外围对象

我的代码:

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)
                    }
                }
            }
        }
    }
}

编译器似乎报告了错误的错误。原因是,
peripheral.name
是可选的,所以使用类似(使用Xcode 12测试)的内容


谢谢,提供默认值解决了问题。那个错误让我怀疑我对斯威夫特的了解。
Button(action: {}){
    Text(peripheral.name ?? peripheral.identifier.uuidString)
}