Reputation: 31
I'm trying to create a List of objects that I received from an API request. The object is Identifiable. The strange thing is, when I iterate over the titles parameter of the object in a ForEach loop, it prints out the expected results. However, when I change it to a List, it doesn't display anything, and it also doesn't give an error or any other indication something went wrong. What am I missing?
Here is my object:
struct Item: Identifiable, Decodable {
let id = UUID()
let title: String?
let canonicalURL: String?
let unread: Bool?
let author: String?
init(title: String?, canonicalURL: String?, unread: Bool?, author: String?){
self.title = title
self.canonicalURL = canonicalURL
self.unread = true
self.author = author
}
}
and here is my view:
@ObservedObject var articlesVM = ArticlesViewModel()
var body: some View {
NavigationView{
ScrollView{
ForEach (articlesVM.entries) { entry in
Text(entry.title!)
} //THIS WORKS
List (articlesVM.entries) { entry in
Text(entry.title!)
} //THIS DOESNT WORK
}.navigationBarTitle("Articles")
.navigationBarItems(trailing: Button(action: {
print("Fetching Data")
self.articlesVM.fetchArticles()
}, label: {
Text("Fetch Articles")
}))
}
}
}
Upvotes: 1
Views: 79
Reputation: 258345
List is itself scrollable. Try the following body:
var body: some View {
NavigationView{
List (articlesVM.entries) { entry in
Text(entry.title!)
}.navigationBarTitle("Articles")
.navigationBarItems(trailing: Button(action: {
print("Fetching Data")
self.articlesVM.fetchArticles()
}, label: {
Text("Fetch Articles")
}))
}
}
Upvotes: 2