Xcode 15.1 canvas preview not working right!

I have same code in Xcode and Playground. But Xcode is not previewing the View the right way. Here is a screenshot of Xcode:

and here is the screen of Playground:

when i run the code (command + R) it's working fine and i get the true preview in the app (just like what i get in Playground). Am I doing something wrong or is it a bug?

Replies

Hi,

Sorry to hear you are having problems getting previews in Xcode working as expected. Looking at the code I would wager that in Xcode's case central.discoveredPeripherals is an empty list. Could you add some additional views like this to help see more details of what is going on:

import SwiftUI

struct ContentView: View {
    @ObservedObject private var central = BLEcentral()
    
    var body: some View {
        VStack{
            Text("pheripherals count: \(Array(central.discoveredPeripherals).count)")
            Text("start list of peripherals")
            ForEach(Array(central.discoveredPeripherals), id: \.key.identifier) { item in
                HStack {
                    Text (item. key.name ?? "unnamed device")
                    Text ("\(item. value)")
                }
            }
            Text("end list of peripherals")
        }
    }
}

#Preview(traits: .fixedLayout(width: 400, height: 800)) {
    ContentView()
}

If that doesn't help then filing a feedback with an attached project will be the best way for us to dig in to this issue further!

@Developer Tools Engineer

Thanks for reviewing my code. I replaced your swift code for the ContentView. The iPhone simulator shows:

pheripherals count: 0

start list of peripherals

end list of peripherals

since it has no access to bluetooth, but the mac preview is not working, although it shows the right view if i run the app.

This is the whole project:

ContentView.swift

import SwiftUI

struct ContentView: View {
    @ObservedObject private var central = BLECentral()
    
    var body: some View {
        VStack{
            Text("pheripherals count: \(Array(central.discoveredPeripherals).count)")
            Text("start list of peripherals")
            ForEach(Array(central.discoveredPeripherals), id: \.key.identifier) { item in
                HStack {
                    Text(item.key.name ?? "unnamed device")
                    Text("\(item.value)")
                }
            }
            Text("end list of peripherals")
        }
    }
}

#Preview() {
    ContentView()
}

BLECentral.swift

import Foundation
import CoreBluetooth

class BLECentral: NSObject, ObservableObject {
    private var centralManager: CBCentralManager?
    @Published var discoveredPeripherals: [CBPeripheral:NSNumber] = [:]
    var onDiscovered: (()->Void)?
    
    override init() {
        super.init()
        self.centralManager = CBCentralManager(delegate: self, queue: nil)
    }
    
    func scanForPeripherals(){
        self.centralManager?.scanForPeripherals(withServices: nil)
    }
}

extension BLECentral: CBCentralManagerDelegate {    
    func centralManagerDidUpdateState(_ central: CBCentralManager) {
        switch central.state {
        case .poweredOn:
            scanForPeripherals()
        case .poweredOff:
            print("Bluetooth is powered off. Please turn it on to use this app.")
        case .unauthorized:
            print("This app is not authorized to use Bluetooth.")
        case .unsupported:
            print("Bluetooth is not supported on this device.")
        case .resetting:
            print("Bluetooth is resetting.")
        case .unknown:
            print("Bluetooth state is unknown.")
        @unknown default:
            print("A new state has come up which isn't handled yet.")
        }
    }
    
    func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String : Any], rssi: NSNumber) {
        if !discoveredPeripherals.keys.contains(peripheral){
            self.discoveredPeripherals[peripheral] = rssi
            onDiscovered?()
        } else {
            discoveredPeripherals[peripheral] = rssi
        }
    }
}

This was a multi-platform application with proper bluetooth and its related privacies added to the project.