
以下是实现代码

设置 Model 数据的结构体
struct MenuSection: Codable, Identifiable {
var id: UUID
var name: String
var items: [MenuItem]
}
struct MenuItem: Codable, Equatable, Identifiable {
var id: UUID
var name: String
var photoCredit: String
var price: Int
var restrictions: [String]
var description: String
var mainImage: String {
name.replacingOccurrences(of: " ", with: "-").lowercased()
}
代码:
struct ContentView: View {
let menu = Bundle.main.decode([MenuSection].self, from: "menu.json")
var body: some View {
NavigationView {
List{
ForEach(menu) { section in
Section (header: Text(section.name)) {
ForEach(section.items) { item in
Text(item.name)
}
}
}
}
.listStyle(GroupedListStyle())
.navigationTitle("菜单")
}
}
}
网友评论