簡體   English   中英

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

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

我正在嘗試僅 select 我的 JSON 文件中的條目之一,並且正在努力這樣做。 到目前為止,這是我的代碼的列表部分:

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

這是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.

在使用 ViewBuilder 的案例之后,您可以從字面上拋出您想要的任何類型的視圖。 文本視圖,嵌套堆棧雖然它有點清晰傳遞預先構建的視圖。 只需將 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()


     }

}

然后把它扔到你的視野里

jsonSelector(using: myID)

如果您添加一些代碼,我將編輯答案以適合您的用例,但這是一般修復

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM