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

Help With Changing a variable in a Array in a List

Forums > SwiftUI

Hello! Can you anyone help regarding the following code:

import SwiftUI

struct ModelData: Hashable, Identifiable {
    let id = UUID()
    var name: String
}

class SampleData:ObservableObject {

    init() {
        self.Model = [
            ModelData(name: "\(name1)"),
            ModelData(name: "\(name2)")
        ]
    }
    @Published var Model: [ModelData] = [ModelData]()
    @Published var name1 = "Tom"
    @Published var name2 = "Chris"
}

struct ContentView: View {
    @StateObject var sampleData = SampleData()
    var body: some View {
        VStack {
            Button("Change") {
                sampleData.name1 = "Chris"
            }
            List(sampleData.Model) { num in
                Text(num.name)
            }
        }

    }
}

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

When I hit the button to change data, why won't the name change? When I change the call to "sampleData.Model[0].name = "Chris"" then it works fine. I simplified the code to help solve the problem. I'm going to be fetching network requests and changing the @Published variables, but can't figure out why it won't work on items in the array.

Thanks so much for your help, and happy holidays!

2      

I'm a little confused, for two reasons.

  1. sampleData.name1 = "Chris" You say the value doesn't change but how do you know it doesn't? You aren't displaying the value of sampleData.name1 anywhere.

  2. You say that you "can't figure out why it won't work on items in the array" but you also say that when you change the code to sampleData.Model[0].name = "Chris" "the it works fine." But that code is changing the items in the array.

Ultimately, it looks like you are expecting that changing name1 (and, presumably, name2) will change the values in sampleData.Model since you use those values to initialize Model in sampleData's init method. But that isn't how it works. The two aren't connected in any way after you use name1 and name2 to set up Model. If you want to change the values in Model, you need to address the array directly, as you do in the code you said works fine.

2      

Yeah, thats exactly what I was trying. I will set everything directly, thanks for your help!

2      

Hacking with Swift is sponsored by RevenueCat

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

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.