Can you specify a “Where” in a SwiftUI ForEach statement?

霸气de小男生 提交于 2020-12-13 03:35:41

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!