你能指定一个“哪里"吗?在 SwiftUI ForEach 语句中? [英] Can you specify a "Where" in a SwiftUI ForEach statement?

查看:28
本文介绍了你能指定一个“哪里"吗?在 SwiftUI ForEach 语句中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 SwiftUI 应用程序中,我有一个定义数据的大类.在应用程序的一部分中,我想查看 ID 与我要查找的内容匹配的记录.我做了如下,但似乎应该更直接.例如允许一个 where 子句.有什么想法吗?

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(更优选,因为没有每个"并且只有一个记录):

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.

谢谢.

推荐答案

你可以在没有 ForEach 的情况下尝试这样的事情:

you could try something like this without the ForEach:

if let item = ItemList.first(where: {$0.id == "xxxx"}) {
   Text(item.description)
}

这篇关于你能指定一个“哪里"吗?在 SwiftUI ForEach 语句中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆