I have data from json for the ListView. But When I add INT to the Models, It show empty value on preview. Does anyone has any idea? Thank you.
struct AppetizersView: View {
@ObservedObject var amodel = AModel()
@Environment(\.presentationMode) var mode: Binding<PresentationMode>
var body: some View {
List(amodel.arecipes) { a in
NavigationLink(
destination: AppetizersDetailView(arecipe:a),
label: {
HStack {
Image(a.image)
.resizable()
.frame(width: 120, height: 90)
VStack(alignment: .leading, spacing: 8) {
tags(tags: a.postType)
Text(a.name)
.bold()
.font(.subheadline)
.lineLimit(1)
Text("Happy Kitchen")
.font(.caption2)
.foregroundColor(Color.gray)
Spacer()
HStack {
Stars(star: a.stars)
}
}
}
})
}
.listStyle(DefaultListStyle())
.navigationBarTitle("Appetizers",displayMode: .inline)
.navigationBarBackButtonHidden(true)
.navigationBarItems(leading: Button(action : {
self.mode.wrappedValue.dismiss()
}){
Image(systemName: "arrow.left")
.foregroundColor(.black)
})
}
}
struct AppetizersView_Previews: PreviewProvider {
static var previews: some View {
AppetizersView()
}
}
struct tags: View {
var tags: Array<String>
var body: some View {
HStack {
ForEach(tags, id: \.self) { e in
Text(e)
.foregroundColor(.pink)
.font(.system(size: 6))
.padding(4)
.overlay(
RoundedRectangle(cornerRadius: 10)
.stroke(Color.pink, lineWidth: 0.5)
)
}
}
}
}
struct Stars: View {
var star: Int
var body: some View {
HStack(spacing: 5) {
ForEach(1...star, id: \.self) { e in
Image(systemName: "star.fill")
.foregroundColor(Color.yellow)
.font(.caption)
}
if star < 5 {
let e = 5 - star
ForEach(1...e, id: \.self) { e in
Image(systemName: "star.fill")
.foregroundColor(Color.gray)
.font(.caption)
}
}
}
}
}
Models
import Foundation
class ARecipe: Identifiable, Decodable {
var aid:UUID?
var name:String
var image:String
var description:String
var prepTime:String
var cookTime:String
var totalTime:String
var servings:String
var stars:Int
var postType:Array<String>
var ingredients:[String]
var directions:[String]
}
json file
[
{
"name": "Beef Chow Fun",
"image" : "beef chow fun",
"description": "Baked eggplant with bread crumbs and lots of cheese. Delicious!",
"prepTime": "25 minutes",
"cookTime": "35 minutes",
"totalTime": "1 hour",
"servings": "10",
"stars": 5
"postType": ["iOS","SwiftUI", "Xcode"],
"ingredients": [
"500g flat rice noodles",
"150g beef",
"1/2 onion",
"120g bean sprouts",
"2 tablespoons of light soy sauce",
"1 tablespoon dark soy sauce",
"1/2 teaspoon salt",
"1 teaspoon sugar",
"Marinades: 2 1/2 teaspoons light soy sauce, 1/4 teaspoon dark soy sauce, 1 teaspoon corn flour, 3/4 teaspoon sugar, 2 teaspoons water, 1/2 teaspoon sesame oil"
],
"directions": [
"Wash the beef first and drain the water. Cut into thin slices horizontally, marinate in the marinade for about 15 minutes, set aside.",
"Heat the oil in a wok, pan-fried the beef on both sides over medium-high heat until it is eighty mature, and then set aside.",
"Heat the wok again, add onions and fry until soft. Add the bean sprouts and stir-fry until cooked, add the flat rice noodles into wok, stir-fry all the ingredients well. Add the seasonings and stir well. Finally, pour the beef and mix well. Serve."
]
}
]