skip to Main Content
@State var myDict: [String: Double] = [:]

var body: some View {
    HStack(alignment: .top) {
        Button(action: {
            self.myDict[self.tag ?? ""] = amountValue
        }) 
    }
}

I have two user, when i select 1st user I add value to myDict, and when I select 2nd user I want to add another value to it (means myDict will have 2 object), but when i select 2nd user @state variable is refresh and have only one value of 2nd user. (means myDict have 1 object)

is there any way so that I can have both the value in dict not only one?

2

Answers


  1. actually, this can happen that for both users self.tag is nil and ("") is used as a key for the dictionary

    self.myDict[self.tag ?? ""] = amountValue
    

    and if this happens you may want to make sure that self.tag is not nil

    I have created a test environment just like your case but I have

    import SwiftUI
    
    struct swiftUIDoubt1: View {
        
    
        @State var myDict: [String: Double] = [:]
        
        var body: some View {
            VStack{
                Button(action: {
                    myDict["Hello, World!"] = 9.99999
                    print(myDict)
                }, label: {
                    Text("(myDict["Hello, World!"] ?? 0)")
                        .padding()
                })
                
                Button(action: {
                    myDict["bye, World!"] = 1.11111
                    print(myDict)
                }, label: {
                    Text("(myDict["bye, World!"] ?? 0)")
                        .padding()
                })
                
            }
            .onAppear(perform: {
                print(myDict)
            })
        }
    }
    

    now as you can see when my screen will appear my dictionary should be empty and an empty dictionary must be printed, as you can see in the image
    console log when the app is first opened

    console log when app is first opened

    device screen when app is first opened

    when I click first button single item will be added and you can see in console log
    single item added

    and when I will click on the second button you can see I have two different items in my dictionary

    two items are added

    let success = "two different items has been added to dictionary"
    

    as @state variable to managed by swift UI and will not change with ui update

    Login or Signup to reply.
  2. Small example : The add in the subview update the dictionnaire for the desired user in parent view

    import SwiftUI
    
    struct UpdateUsersDict: View {
        @State var myDict: [String: Double] = [:]
        @State var amount: Double = 100
        var body: some View {
            VStack {
                HStack {
                    OneUserView(myDict: $myDict, amount: amount, tag: "user 1")
                    OneUserView(myDict: $myDict, amount: amount, tag: "user 2")
                    OneUserView(myDict: $myDict, amount: amount, tag: "user 3")
                }
                HStack {
                    Text("(myDict["user 1"] ?? -1)")
                    Text("(myDict["user 2"] ?? -1)")
                    Text("(myDict["user 3"] ?? -1)")
                }
            }
        }
    }
    
    struct OneUserView: View {
        @Binding var myDict: [String: Double]
        @State var amount: Double
        var tag: String
    
        var body: some View {
            HStack(alignment: .top) {
                Button(tag, action: {
                    self.myDict[self.tag] = amount
                })
            }
        }
    }
    
    struct UpdateUserDict_Previews: PreviewProvider {
        static var previews: some View {
            UpdateUsersDict()
        }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search