0

I'm developing an iOS application using SwiftUI, and I have a custom view called DashboardView which takes an @ObservedObject of type SessionStore.

The problem: the preview does not show at all in Xcode. There's no compile-time error, but I get a message like "Cannot preview this file" or the canvas remains blank.

Here's the relevant part of the code, including the preview:

import SwiftUI

struct DashboardView: View {
    @ObservedObject var store: SessionStore
    @State private var showCreate = false
    @State private var showAddClient = false
    @State private var selectedSessionID: UUID?

    // Computed properties and body content...
}

// MARK: - Preview

#Preview {
    let mockStore = SessionStore()
    let session = Session(
        id: UUID(),
        date: Date(),
        title: "Test Session",
        people: [
            Person(id: UUID(), name: "Alice", debts: [
                Debt(id: UUID(), description: "Coffee", amount: 3.5),
                Debt(id: UUID(), description: "Sandwich", amount: 5.0)
            ])
        ]
    )
    mockStore.sessions = [session]
    return DashboardView(store: mockStore)
}


I expected the preview to show a basic preview of `DashboardView` with mock data from `mockStore`.

What I’ve tried:
- Creating a mock `SessionStore` with sample data in the preview
- Confirmed that `Session`, `Person`, and `Debt` models conform to `Identifiable`, `ObservableObject`, and `Codable`
- Moved all the code into one file and into separate files
- Cleaned the project, restarted Xcode, rebuilt the preview
- Checked for missing initializers or broken views

I still don’t understand why the preview won’t render. No error, just a blank or unavailable canvas. Any help or guidance would be much appreciated.
1
  • In my tests, your code works well for me in Xcode Preview, on macOS Sequoia 15.5 using Xcode 16.4, target iOS-18.5 devices, eg iPhone 16 pro. What system are you using and targeting? Note, NavigationView has been deprecated for years, use NavigationStack instead. Commented May 16 at 3:19

1 Answer 1

0

We use a closure to set up the mockStore. This is because SwiftUI’s #Preview macro doesn’t let us do multiple top-level statements. It only lets us return a View. So, we wrap the setup code in a closure and call it right away with (). This way, we can do all the setup and then return the fully initialized DashboardView.

import SwiftUI

struct DashboardView: View {
    @ObservedObject var store: SessionStore
    @State private var showCreate = false
    @State private var showAddClient = false
    @State private var selectedSessionID: UUID?

    // Computed properties and body content...
  
  var body: some View {
    VStack {
      Text(store.sessions[0].id.uuidString)
    }
  }
}

// MARK: - Preview

#Preview {
    let mockStore: SessionStore = {
        let store = SessionStore()
        let session = Session(
            id: UUID(),
            date: Date(),
            title: "Test Session",
            people: [
                Person(id: UUID(), name: "Alice", debts: [
                    Debt(id: UUID(), description: "Coffee", amount: 3.5),
                    Debt(id: UUID(), description: "Sandwich", amount: 5.0)
                ])
            ]
        )
        store.sessions = [session]
        return store
    }()

    return DashboardView(store: mockStore)
}

enter image description here

Sign up to request clarification or add additional context in comments.

1 Comment

sorry but I see same code as it was in the morning(for me)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.