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

SOLVED: Passing Properties Between Views

Forums > SwiftUI

I'm going to try and break this down in as simple terms as possible.

Let's say I have a Struct with this property in it

@State private var selectedGifName: String?

When a user taps an item on the screen, this property updates with a simple string containing the file name of a gif image.

Now, I have a Class in a different file of my app that makes a POST request to an API. One of the parameters of that POST request takes the gif image name contained in that State variable from the other file's Struct.

What is the most appropriate way to pass the value of selectedGifName from my Struct, into the other file's Class where the POST request is being done.

I've tried:

-Calling the Struct directly as a new variable inside the Class, but I'm required to include parameters for every property of that Struct which isn't necessary.

-Messed with @StateObject assignements but this doesn't work when a Struct has something a Class wants.

The connections between View files and ViewControllers is making my brain hurt. That's mainly what this comes down to. When structuring a good MVVM app, I'm really having problems understanding how different Structs and Classes can pass details about properties to one another.


Probably as a parameter to the method on your class that makes the API call.

You should have a ViewModel ObservableObject class that handles the API stuff and pass it a parameter from the View struct when the @State variable changes.

The general outline of that would be something like:

class ViewModel: ObservableObject {
    @Published var apiResult: Whatever

    func fetchAPIData(for param: ParamType) {
        //do API stuff and assign result to apiResult

struct ExampleView: View {
    @StateObject var viewModel: ViewModel()
    @State private var selectedGifName: String?

    var body: some View {
        TextField("GIF Name", text: $selectedGifName)
            .onChange(selectedGifName) { _ in
                if let selectedGifName = selectedGifName {
        //other View stuff that uses viewModel.apiResult

The connections between View files and ViewControllers is making my brain hurt.

You're using SwiftUI, you don't need to think about ViewControllers at all.


Thank you RB.

Would you mind giving me some more insight about your last statement that SwiftUI means I don't need to worry about ViewControllers?


UIKit (and other pre-SwiftUI Apple frameworks) uses ViewControllers as part of the MVC pattern. ViewControllers sit between the model and the view and coordinates things between them.

SwitUI does away with ViewControllers. Instead it uses declarative View structs, which are essentially blueprints for how your interface should be rendered onscreen. There is no ViewController to mediate between a View and a model in SwiftUI; instead, it uses data binding and reactive programming concepts to handle the kinds of things ViewControllers took care of before.

You only need to worry about ViewControllers in SwiftUI when bridging between it and older stuff that doesn't have an equivalent (yet). Then you would use a (UI|NS)ViewControllerRepresentable to wrap the old ViewController and allow it to work in the newer SwiftUI environment.


Hacking with Swift is sponsored by Superwall

SPONSORED Superwall lets you build & test paywalls without shipping updates. Run experiments, offer sales, segment users, update locked features and more at the click of button. Best part? It's FREE for up to 250 conversions / mo and the Superwall team builds out 100% custom paywalls – free of charge.

Learn More

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.