问题
In a SwiftUI application, I have a large class defining data. In part of the app, I would like to look at one record where the ID matches what I am looking for. I have done it as follows, but there seems it should be more straight forward. Such as allowing a where clause. Any ideas?
ForEach (ItemList) { item in
if (item.itemId = thisId) {
Text(item.description)
}
}
So rather than going thru the entire list to find the match, I'd like to:
ForEach (ItemList WHERE ID = XXX) {
OR (more preferably, since there is no "Each" and its only one record):
Text(ItemList[itemId].description)
Can't find any specific info on this, any assistance is appreciated.
Thank you.
回答1:
you could try something like this without the ForEach:
if let item = ItemList.first(where: {$0.id == "xxxx"}) {
Text(item.description)
}
来源:https://stackoverflow.com/questions/64637549/can-you-specify-a-where-in-a-swiftui-foreach-statement