简体   繁体   English

如何从 swift 中的 JSON 文件中 select 指定 id?

[英]How do I select a specific id from a JSON file in swift?

I'm trying to just select one of the entries in my JSON file and am struggling to do so.我正在尝试仅 select 我的 JSON 文件中的条目之一,并且正在努力这样做。 Here is the list part of my code so far:到目前为止,这是我的代码的列表部分:

    var body: some View {
        
        NavigationView {
            Group {
                List {
                    ForEach(regions) { regions in
                   NavigationLink(destination: RegionsListView(regions: regions)) {
                       RegionButtonView(regions: regions)
                   }
              }
        }
            }
        }
        }

And here is the if statement :这是if statement

    let regions: Region
    
    var body: some View {
        if regions.id != nil {
            SCaliView()
        } else if regions.id != nil {
            BAView()
        }
    }
}
struct Region: Codable, Identifiable {
    let id: Int
    let name: String
    let image: String

}

I want to make it so that when a certain id is selected, it will go to a certain view. I want to make it so that when a certain id is selected, it will go to a certain view.

You can literally throw whatever kind of view you'd like after the case using a ViewBuilder.在使用 ViewBuilder 的案例之后,您可以从字面上抛出您想要的任何类型的视图。 Text Views, Nested Stacks although its a little clear passing pre-constructed views.文本视图,嵌套堆栈虽然它有点清晰传递预先构建的视图。 Just replace the Int with whatever you id type is, although Int should work and then modify the case statements to handle your different ids只需将 Int 替换为您的 id 类型,尽管 Int 应该可以工作,然后修改 case 语句以处理您的不同 id

 @ViewBuilder func jsonSelector(using id: Int) -> some View {
      switch id {
         case 0:
      ViewA()
         case 1: 
      ViewB()
         case 2: 
      ViewC()
         case 3: 
      ViewD()


     }

}

Then just throw it in your view然后把它扔到你的视野里

jsonSelector(using: myID)

If you add some code, I'll edit the answer to fit your use case but this is a general fix如果您添加一些代码,我将编辑答案以适合您的用例,但这是一般修复

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM