13

i'm trying to using a variable and modify inside the foreach of swiftui.

below are the code i'm using. please let me know how can i modify the value inside the body of the view.

struct SearchView : View {
var chatmember = ChatMember(with: JSON())

var body: some View{
    VStack(spacing: 10){
        VStack{
            prefView()
                .padding(.bottom, 30)
            Text("Tap to start making friend")
        }
        ScrollView(.horizontal, content: {
            HStack(spacing: 10) {
                ForEach(userDataList) { user in
                     chatmember = ChatMember(hashValue: 0, email: "", fullName: "", online: "", userId: user.userId, userImage: "", deviceToken: "", deviceType: .iOS, deviceId: "")
                    NavigationLink(destination: ChatView(chatMember: self.chatmember)){
                        SearchUser()
                    }
                }
            }
            .padding(.leading, 10)
        })
            .frame(width: 300, height: 400)

        Spacer()
    }
    .navigationBarTitle("Profile",displayMode: .inline)
    .onAppear {
        self.userList()
    }
}


@State var userDataList = [UserModel]()
func userList() {
    databaseReference.child(DatabaseNode.Users.root.rawValue).observe(.value) { (snapShot) in
        if snapShot.exists(){

            if let friendsDictionary = snapShot.value{

                for each in friendsDictionary as! [String : AnyObject]{
                    print(each)
                    if let user = each.value as? [String : Any]{
                        let data = UserModel(userId: JSON(user["userId"] ?? "").stringValue, name: JSON(user["name"] ?? "").stringValue)
                        print(data)
                        self.userDataList.append(data)
                    }
                }
            }
        }
    }
}

}

below are the error message i'm getting:

'Int' is not convertible to 'CGFloat?'

But this error message is not look correct for me. this error i'm getting because i tried to modify chatmember inside of foreach.

Thanks

Sourav Mishra
  • 501
  • 4
  • 21
  • see https://stackoverflow.com/questions/60230457/how-to-disambiguate-a-foreach-loop-in-xcode-with-swiftui/60232370#60232370 – user3441734 Feb 16 '20 at 21:29
  • How did you manage to get the current user element outside the body? I am new to SwiftUI and couldn't figure it. – Vidhya Sri Mar 30 '21 at 08:29

1 Answers1

3

You can't modify the chatmember in your body function, it should be done in an action clouser or outside of the body function scope.

for populate your chatmember you can try this:

add .onAppear to your destination and set your chatmember

NavigationLink(destination: ChatView(chatMember: self.chatmember).onAppear {
                     chatmember = ChatMember(hashValue: 0, email: "", fullName: "", online: "", userId: user.userId, userImage: "", deviceToken: "", deviceType: .iOS, deviceId: "")
                    }){
                        SearchUser()

don't forget to set your `chatmember` to @State
Mac3n
  • 4,189
  • 3
  • 16
  • 29
  • can you give an example for the same? – Sourav Mishra Feb 16 '20 at 15:57
  • you can define a function and make your list in your function. for your struct define init() and call your function in it – Mac3n Feb 16 '20 at 16:00
  • can you please help me coding example. i want to modify the value and pass it to new view like previously we did in 'did select for row' when we tap on cell item. something similar to that i want to do that. please help me with coding example i'm new to swiftUI – Sourav Mishra Feb 16 '20 at 16:06
  • Thanks i will implement this and check it. can you please check this question also https://stackoverflow.com/questions/60250383/horizontal-scrollview-is-not-working-swiftui – Sourav Mishra Feb 16 '20 at 16:57
  • value is not reflecting to my chat view. it's not working – Sourav Mishra Feb 17 '20 at 14:59