struct ContentView: View {

@State private var isShowingRestaurant = false

let posts: [Post] = [
    .init(id: 0, username: "asdas", text: "mniam mniam mniam", imageName: "logo"),
    .init(id: 1, username: "asdas", text: "mniam mniam mn", imageName: "sushi"),
    .init(id: 2, username: "asdas", text: "mniam mniam mn", imageName: "sushi"),
    .init(id: 3, username: "asdas", text: "mniam mniam mn", imageName: "sushi"),
    .init(id: 4, username: "asdas", text: "mniam mniam mn", imageName: "sushi"),
    .init(id: 5, username: "asdas", text: "mniam mniam mn", imageName: "sushi"),
    .init(id: 6, username: "asdas", text: "mniam mniam mn", imageName: "sushi")
  ]
}


如何访问变量,例如,我只想显示图像名称为“徽标”的帖子。我写了帖子[imageName =“ logo”],但不起作用。

最佳答案

您需要使用filter

let logoPosts = posts.filter { $0.imageName == "logo" }

关于arrays - 如何在循环Foreach中编写条件,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/59707812/

10-10 17:25