project/개발 업무

[XCode 15.0 beta] Preview Macro Bug

lgvv 2023. 6. 8. 12:06

Preview Macro Bug

 

🚨 Bug

 

CompileDylibError: Failed to build ContentView.swift

 

Compiling failed: main actor-isolated let 'previewContainer' can not be referenced from a non-isolated context

 

 

✅ Solution

 

1.

#Preview {
    MainActor.assumeIsolated {
        ContentView()
            .frame(minWidth: 500, minHeight: 500)
            .modelContainer(previewContainer)
    }
}

 

2.

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
            .modelContainer(previewContainer)
    }
}

 

3.

#Preview { @MainActor in
    ContentView()
        .frame(minWidth: 500, minHeight: 500)
        .modelContainer(previewContainer)
}

 

5.

#Preview {
    MainActor.assumeIsolated {
        ContentView()
            .frame(minWidth: 500, minHeight: 500)
            .modelContainer(previewContainer)
    }
}

 

 

 

[forum link]

https://developer.apple.com/forums/thread/731134 

 

Error after adding previewContaine… | Apple Developer Forums

I asked Chatgpt for this question, and it told me this: #Preview { MainActor.assumeIsolated { ContentView() .frame(minWidth: 500, minHeight: 500) .modelContainer(previewContainer) } } It worked.

developer.apple.com