UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

SwiftUI Preview and @EnvironmentObject

Forums > SwiftUI

Hi all,

First post. Love the site & forum. I've googled away on this question but no sign of an answer anywhere

Is there any way to set the value of variables within an EnvironmentObject when generating a preview.

Toy example below. How do I generate a preview with myEnvObject.someBool = false.

This is easy to do if someBool is a local variable within the View's own struct but I don't see how I would do it when the variable belongs to an environment object.

Swift Class...

import Foundation

class SomeObject: NSObject, ObservableObject {
     @Published var someBool: Bool = true

     func someFunction() {
         someBool = false
     }
}

SwiftUI View...

import SwiftUI

struct ContentView: View {
    @EnvironmentObject var myEnvObject: SomeObject

    var body: some View {
        if myEnvObject.someBool == true {
            Text("This will show up in Preview")
        } else {
            Text("How do I generate a Preview showing this text?")
        }
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView().environmentObject(SomeObject())

    }
}

Many thanks, Jim

2      

Hi Jim

Add this in ContentView_Previews

struct ContentView_Previews: PreviewProvider {
    static let myEnvObject = SomeObject()

    static var previews: some View {
        ContentView()
            .environmentObject(myEnvObject)
    }
}

then it should work in preview. The Preview will only show the first bool eg true however if you want the 2nd text to show then change the if to false

6      

Thanks so much @NigelGee

I am assuming from this that there is no "good" way to set a property of an environment object to a non-initial value in a preview. The only options seem to be temporarily editing either the View or the Environment Object (which then makes them incorrect) just to support previewing the view.

My real-world View has multiple branches, so this wouldn't work as a practical solution.

Thanks anyway

2      

Try this:

struct ContentView_Previews: PreviewProvider {

    static var previews: some View {

        ContentView().environmentObject({ () -> SomeObject in
                    let envObj = SomeObject()
                    envObj.someBool = true
                    return envObj
                }() )

    }
}

or better:

struct ContentView_Previews: PreviewProvider {

    static var previews: some View {

        Group {

            ContentView().environmentObject({ () -> SomeObject in
                let envObj = SomeObject()
                envObj.someBool = true
                return envObj
            }() )

            ContentView().environmentObject({ () -> SomeObject in
                let envObj = SomeObject()
                envObj.someBool = false
                return envObj
            }() )

        }
    }
}

6      

@Bnerd  

@MateMuller

You are a lifeSaver, I was trying hours to figure a way to make my previews to work..

This worked like a charm!!!

struct ContentView_Previews: PreviewProvider {

    static var previews: some View {

        ContentView().environmentObject({ () -> SomeObject in
                    let envObj = SomeObject()
                    envObj.someBool = true
                    return envObj
                }() )

    }
}

1      

Hacking with Swift is sponsored by Essential Developer

SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until April 28th.

Click to save your free spot now

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.