[英]How do I get my Swift code to read a specific piece of information from the JSON file
[英]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.