Reputation: 717
I'm trying to wrestle SwiftUI and either something is corrupt with my version of Xcode or I'm doing something wrong. I'm trying to loop through an array of social networks and it's not looping through. The error messages I get are:
Referencing initializer 'init(_:content:)' on 'ForEach' requires that 'SocialNetwork' conform to 'View'
Generic struct 'List' requires that 'SocialNetwork' conform to 'View'
struct SocialNetwork: Identifiable {
let id = UUID()
let type: NetworkType
let url: String
let icon: String
}
struct ChartView: View {
var networks = [SocialNetwork(type: .Instagram, url: "", icon: "")]
var body: some View {
GeometryReader { geometry in
VStack {
List {
ForEach(networks) { net in
net
}
}
}
}
}
}
I'm confused as to why a simple struct would need to conform to View when it's just an array of identifiable?
Upvotes: 1
Views: 745
Reputation: 73
Since the type SocialNetwork is not conforming to View the compiler warning is correct. You could try to display the url of each SocialNetwork in your ForEach by replacing net
with Text(net.url)
. Just as an example to make it compile
Upvotes: 1
Reputation: 257493
The ForEach
is a view container, so inside it there should be some view, but your net
is a model (instance of SocialNetwork
). Put there some list row view presenting one network, like
List {
ForEach(networks) { net in
Label(net.url, image: net.icon)
}
}
Upvotes: 3