I'm trying to follow along with this chat app tutorial and the syntax for the ForEach function has been updated in SwiftUI. Can you please help me make this list successfully compile using SwiftUI?
import SwiftUI
struct ChatMessage : Hashable {
var message: String
var avatar: String
}
struct ContentView : View {
var messages = [
ChatMessage(message: "Hello world", avatar: "A"),
ChatMessage(message: "Hi", avatar: "B")
]
var body: some View {
List {
ForEach(messages.identified(by: \.self)) {
Text($0.avatar)
Text($0.message)
}
}
}
}
I tried changing the ForEach to the updated syntax:
var body: some View {
List {
ForEach(messages, id: \.self) {
Text($0.avatar)
Text($0.message)
}
}
}
However, I'm receiving an error message: "Unable to infer complex closure return type; add explicit type to disambiguate"
Textviews in either anHStackor aVStack. Basically, yourForEachis like creating a table cell. In a basic view you can't just place twoTextsubviews without a stack of some sort - and neither can you in aForEach. - dfd