Post

Replies

Boosts

Views

Activity

Reply to Working of List and ForEach in SwiftUI
One more use is that, When u add static elements like button within the List, it wont be updated when view gets invalidated through a @Published property. We need ForEach within the List to get it updated.   @ObservedObject private(set) var viewModel: SongsViewModel       init() {     viewModel = SongsViewModel()   }       var body: some View {     VStack {       List {         ForEach(viewModel.songsList, id:\.self) { song in           Button(action: {             viewModel.didSelect(song: song)           }) {             FlatTileView(model: song)               .frame(height: 60)           }         }       }     }   } } In the above example, ViewModel gets updated when user gives us access to user's Media Library. If i removed ForEach, the elements within the list will not be updated.
Jun ’21