How to use array of strings in ForEach in SwiftUI

Issue #483

Every item in list must be uniquely identifiable

1
2
3
4
5
6
7
8
9
10
11
12
List {
ForEach(books, id: \.bookId) { book in
NavigationLink(destination:
BookView(book: book)
.navigationBarTitle(book.name)
) {
VStack {
Text(book.name)
}
}
}
}

In case of primitive, we can just provide id to conform to Identifiable

1
2
3
4
5
extension String: Identifiable {
public var id: String {
return self
}
}

Comments