Viktor Goleš
Viktor Goleš

Reputation: 33

How to append data to an array from another view (swiftUI)?

In the first view I have a textfield and a date picker. How do I append that information to the array in another view so that the inputed information is added to a list? I have this struct:

import Foundation

struct Friend: Identifiable {
    let name: String
    let bday: String
    let id = UUID()
}

and this list:

import SwiftUI

struct FriendView: View {
    
    @State private var friends = [Friend]()
    
    var body: some View {
        List(friends) { Friend in
            HStack {
                Text(Friend.name)
                Spacer()
                Text(Friend.bday)
            }
        }
    }
}

struct FriendView_Previews: PreviewProvider {
    static var previews: some View {
        FriendView()
    }
}

and this is the view where I want to append the information from the form to the friends array but I keep getting "Value of type 'AddFriendView' has no member 'friends'" error at the addFriend function.

struct AddFriendView: View {
    @State var input: String = ""
    @State var date: Date = Date()
    
    

    var dateFormatter: DateFormatter {
        let formatter = DateFormatter()
        formatter.dateStyle = .medium
        return formatter
    }
    
    
    func addFriend () {
        self.friends.append(Friend(name: "name", bday: "birthday")) //ERROR POPS UP HERE
        dump(friends)
    }

    var body: some View {
        VStack {

            Spacer()
            
            TextField("Enter name", text: $input)
                .foregroundColor(.yellow)
                .padding(.all)
                .multilineTextAlignment(.center)
                .disableAutocorrection(true)
                .font(.largeTitle)
                .onTapGesture {
                  self.hideKeyboard()
                }
            
            Text("was born on a date")
                .font(.title)
                .foregroundColor(.yellow)
            
            DatePicker(
                "",
                selection: $date,
                displayedComponents: [.date]
            )
                .labelsHidden()
            
            Spacer()
            
            Button(
                "Save birthday"
            ) {
                self.addFriend()
                //print("\(self.input) \t \(self.dateFormatter.string(from: self.date))")
                self.input = ""
            }
                .padding(.all, 50)
                .accentColor(.white)
                .background(Color.yellow)
                .cornerRadius(25)
           // Spacer()
        }
    }
}

Any help would be greatly appreciated. Thank you in advance

I tried adding @Binding var friends but then I get "Type annotation missing in pattern" error and also @Binding var friends: [Friend] but then I get "Missing argument for parameter 'friends' in call" error at the end here:

struct AddFriendView_Previews: PreviewProvider {
    static var previews: some View {
        AddFriendView()
    }
}

Upvotes: 2

Views: 965

Answers (1)

vadian
vadian

Reputation: 285059

@Binding var friends: [Friend]

is correct. And the missing argument in the preview can be accomplished by providing a constant, an empty array

struct AddFriendView_Previews: PreviewProvider {
    static var previews: some View {
        AddFriendView(friends: .constant([]))
    }
}

or a sample friend

struct AddFriendView_Previews: PreviewProvider {
    static var previews: some View {
        AddFriendView(friends: .constant([Friend(name: "John Doe", bday: "Jan 1, 2000")]))
    }
}

Upvotes: 1

Related Questions