Questions tagged [swiftui-previews]
45 questions
3
votes
1 answer
SwiftUI Preview with Core Data: invalid NSEntityDescription
I'm trying to have my SwiftUI Previews work with an in memory Core Data Stack (from Xcode Template). As soon as I call Entity.entity(), I get the following error message:
let context = PersistenceController.preview.container.viewContext
let…

alpennec
- 1,864
- 3
- 18
- 25
2
votes
1 answer
How to show SwiftUI preview in landscape
.previewLayout(.fixed(width: 480, height: 320)) has no effect in Xcode 13.4 and Xcode 14 beta 3
The preview is shown in portrait vs expected landscape

Anton Tropashko
- 5,486
- 5
- 41
- 66
2
votes
1 answer
SwiftUI -> Thread 1: Fatal error: No ObservableObject of type ModelData found
I created an app with SwiftUI and when I try to display a button this error message appears:
Thread 1: Fatal error: No ObservableObject of type ModelData found. A View.environmentObject(_:) for ModelData may be missing as an ancestor of this…

Shehran M.
- 71
- 4
2
votes
2 answers
SwiftUI Problem with main attribute only in Previews
I am running a SwiftUI app (Xcode 12.3) using the App approach:
@main
struct ThingsApp: App {
...
}
This works as expected both on simulators and my device. But it does not work in previews: There, I get the error of 'main' attribute cannot be used…

cgold
- 4,075
- 1
- 12
- 13
2
votes
1 answer
Can SwiftLint ignore classes (not files) by suffix? Namely SwiftUI _Preview classes?
Most of my SwiftUI classes have an auto generated (from template) ClassName_Previews class at the bottom of the file.
I'd like SwiftLint to ignore anything ending in _Previews for ALL rules. Or anything that extends PreviewProvider.
I know how to…

dominiquealexi
- 21
- 4
1
vote
2 answers
How to print in SwiftUI Previews?
I know how to print in a SwiftUI view. I would like to do the same within a SwiftUI preview so that I don't need to run the app in the simulator to see the logs.
For example here I'm trying to print a view's frame size:
struct ContentView: View {
…

Senseful
- 86,719
- 67
- 308
- 465
1
vote
1 answer
How to optimize SwiftUI previews?
Is There a way to optimize SwiftUI preview builds so it can be done faster ?
I have a very big project and each time I need to do a View preview it would take forever building the entire project
For example I don't want some pods to be compiled on…

Marchal
- 286
- 2
- 16
1
vote
2 answers
Making Complex JSON Data Codable with Swift
I know this isn't totally SwiftUI related but please bear with me.
Using this excellent video by Karin Prater https://www.youtube.com/watch?v=ZHK5TwKwcE4
I have built an app using the spaceX API here : https://api.spacexdata.com/v4/launches using…

jat
- 183
- 3
- 14
1
vote
1 answer
Preview isolated SwiftUI views in a widget with a custom size
One of the big strengths of SwiftUI is modularity: I don't need to build one large view (controller), I can easily compose my main view from many smaller view components. But developing these view components efficiently requires working live…

Mischa
- 15,816
- 8
- 59
- 117
1
vote
0 answers
SwiftUI dismiss() does not work on preview
I am making a TODO app on Xcode (ver 13.4) on mac.
When TodoEditor's "Add" button is tapped, the preview screen should show a ContentView and the ContentView should show updated Todo contents but it shows the ContentView and the updated Todo…

peridotite
- 115
- 3
1
vote
1 answer
SwiftUI preview fails to build when using enum as namespace
Since Swift has no real first-class namespace support, enums are often used as a replacement. When doing so, SwiftUI previews don't build anymore:
import SwiftUI
enum Namespace { }
extension Namespace {
struct ContentView: View {
var…

Gereon
- 17,258
- 4
- 42
- 73
1
vote
1 answer
Preview SwiftUI private non-default @State
SwiftUI state properties should be declared as private. This is good for encapsulating their values from containing views, but prevents a preview from setting a non-default state using the default initializer. For example, this doesn't…

Edward Brey
- 40,302
- 20
- 199
- 253
1
vote
1 answer
SwiftUI Previews w/Mock Realm
I am running into a problem trying to use an in memory realm configuration with previews. When the preview executes it returns an error. The diagnostics gives the following error:
MessageError: Connection interrupted
I have the following class…

Cosmtar
- 516
- 5
- 14
1
vote
1 answer
Xcode SwiftUI Previews build settings
I am running a pre-build script using scheme. My script has enabled option to provide build settings from target that is being built. This script also gets executed when project is built for SwiftUI Previews. I need to know inside this script if the…

Piotr Tobolski
- 1,326
- 7
- 22
1
vote
2 answers
How to create multiple previews of cells based on test data in SwiftUI with a foreach
I am trying to display multiple previews of the card view (Apple's SwiftUI tutorial). I managed to do it manually using the Group struct.
struct CardView_Previews: PreviewProvider {
static var previews: some View {
Group {
…

Gael
- 459
- 3
- 18