Posts

Post not yet marked as solved
0 Replies
232 Views
I am creating a Medical App using SwiftUI. I am trying to make a treatment view that provides a different set of answers (fetched from a .json) based on the answer to a certain set of questions. I have tried this (see below) but I am experiencing errors with it. I figured it would be best to get some other opinions before diving down this rabbithole. Full code is on GitHub: https://github.com/e-kendall/ReGen-Responder/ // // BrokenBonesQ1.swift // ReGen Responder // // Created by Eamon Kendall on 3/8/2023. // import SwiftUI struct BrokenBonesQ1: View { @State private var BrokenBonesQ1YES: Bool = false @State private var BrokenBonesQ1NO: Bool = false @Binding var BrokenBonesQ2YES: Bool @Binding var BrokenBonesQ2NO: Bool @Binding var stepIDS: [String] @Binding var currentStepIndex: Int var body: some View { NavigationStack { VStack { Spacer() Text("You've called for help for a Broken Limb.") .font(.title) Text("Please answer the following questions") .font(.title2) Spacer() Text("Are there any protruding bones?") .font(.title3) ZStack { RoundedRectangle(cornerRadius: 16) .frame(width: 451, height: 58) .foregroundColor(.gray) NavigationLink("Yes", destination: BrokenBonesQ2(), isActive: $BrokenBonesQ1YES) .foregroundColor(.white) } NavigationLink(destination: TreatmentView(), isActive: $BrokenBonesQ1NO) { ZStack { RoundedRectangle(cornerRadius: 16) .frame(width: 451, height: 58) .foregroundColor(.gray) Text("No") .foregroundColor(.white) } .simultaneousGesture(TapGesture().onEnded { BrokenBonesQ1NO = true if BrokenBonesQ1NO { stepIDS = ["4", "5", "6"] currentStepIndex += 1 } }) } Spacer() } .navigationBarBackButtonHidden() } } } struct BrokenBonesQ1_Previews: PreviewProvider { @State static var brokenBonesQ2YESExamples = false @State static var brokenBonesQ2NOExamples = false // Set it to false initially @State static var stepIDExamples = ["5", "6"] @State static var currentStepIndex = 0 // Set it to the initial step index static var previews: some View { BrokenBonesQ1( BrokenBonesQ2YES: $brokenBonesQ2YESExamples, BrokenBonesQ2NO: $brokenBonesQ2NOExamples, stepIDS: $stepIDExamples, currentStepIndex: $currentStepIndex // Pass the currentStepIndex binding ) .previewInterfaceOrientation(.landscapeRight) } } // // TreatmentView.swift // ReGen Responder // // Created by Eamon Kendall on 6/8/2023. // import SwiftUI struct TreatmentView: View { @EnvironmentObject var modelData: ModelData @State private var currentStepIndex: Int = 0 @State private var stepIDS: [String] = ["1", "6", "9"] var body: some View { NavigationStack { HStack { VStack(alignment: .leading) { Text(modelData.treatmentSteps[currentStepIndex].direction) .font(.system(size: 36, weight: .semibold)) .multilineTextAlignment(.leading) Text(modelData.treatmentSteps[currentStepIndex].description) .font(.system(size: 24, weight: .medium)) if let materialLocation = modelData.treatmentSteps[currentStepIndex].materialLocation { HStack { ZStack { Circle() .foregroundColor(.red) .frame(width: 224, height: 224) Text(materialLocation) .foregroundColor(.white) .font(.system(size: 84, weight: .medium)) } .padding() } .frame(width: 490) } Spacer() ZStack { RoundedRectangle(cornerRadius: 16) .frame(width: 451, height: 58) .foregroundColor(.gray) Button("Next") { showNextStep(withStepIDs: stepIDS) } .foregroundColor(.white) } .padding() Text("If the patients condition worsens or you are ever in doubt, Dial 000.") .font(.headline) } .padding() Image(modelData.treatmentSteps[currentStepIndex].image) .resizable() .aspectRatio(contentMode: .fit) .cornerRadius(10) .padding() .navigationBarBackButtonHidden() } .padding() } } func showNextStep(withStepIDs stepIDs: [String]) { guard let currentIndex = modelData.treatmentSteps.firstIndex(where: { $0.id == stepIDS[currentStepIndex] }) else { return } let validSteps = modelData.treatmentSteps.enumerated().filter { index, step in stepIDs.contains(step.id) && index > currentIndex } if let nextStep = validSteps.first { currentStepIndex = nextStep.offset } } } struct TreatmentView_Previews: PreviewProvider { static var previews: some View { TreatmentView() .environmentObject(ModelData()) .previewInterfaceOrientation(.landscapeRight) } }
Posted
by E-K.
Last updated
.
Post not yet marked as solved
1 Replies
719 Views
Hello, I've completed the Landmarks App Apple Developer tutorial, since finishing it I decided to create a project using the .JSON code from the project. I've wrote the code in an identical layout as the tutorial, however I am receiving the error - Couldn't parse lesssonData.json as array - when trying to load a preview and the app builds successfully but crashes when I launch it. Below is the code, any help is appreciated! LessonList.swift import SwiftUI struct LessonList: View { var lesson: Lesson var body: some View { VStack { List { Section { ForEach(lessons, id: \.self) { lesson in Label(lesson.name, systemImage: "house") } } Section { Label("Hello World!", systemImage: "globe") } } } } } struct LessonList_Previews: PreviewProvider { static var lessons = ModelData().lessons static var previews: some View { LessonList(lesson: lessons[0]) } } ModelData.swift import Foundation import Combine final class ModelData: ObservableObject { @Published var lessons: [Lesson] = load("lessonData.json") } var lessons: [Lesson] = load("lessonData.json") func load<T: Decodable>(_ filename: String) -> T { let data: Data guard let file = Bundle.main.url(forResource: filename, withExtension: nil) else { fatalError("Couldn't find \(filename) in main bundle.") } do { data = try Data(contentsOf: file) } catch { fatalError("Couldn't load \(filename) from main bundle:\n\(error)") } do { let decoder = JSONDecoder() return try decoder.decode(T.self, from: data) } catch { fatalError("Couldn't parse \(filename) as \(T.self):\n\(error)") // Error on this Line } } lessonData.JSON [ { "id":"1001", "lessonNo":"Lesson 1", "name":"Introduction to Photography", "category":"Introduction", }, { "id":"1002", "lessonNo":"Lesson 2", "name":"Negative and Positive Space", "category":"Introduction", }, { "id":"1003", "lessonNo":"Lesson 3", "name":"Introduction to Camera Angles", "category":"Introduction", }, { "id":"1004", "lessonNo":"Lesson 4", "name":"Lighting and Framing", "category":"Beginners", }, { "id":"1005", "lessonNo":"Lesson 5", "name":"Still Photography", "category":"Beginners", }, { "id":"1006", "lessonNo":"Lesson 6", "name":"Motion Photograhy", "category":"Beginners", }, { "id":"1007", "lessonNo":"Lesson 7", "name":"Aperture and F-Stops", "category":"Intermiediate", }, { "id":"1008", "lessonNo":"Lesson 8", "name":"Shutter Speeds", "category":"Intermiediate", }, { "id":"1009", "lessonNo":"Lesson 9", "name":"Advanced Framing", "category":"Advanced", }, { "id":"1010", "lessonNo":"Lesson 10", "name":"Advanced Aperture, F-Stops and Shutter Speeds", "category":"Advanced", }, ]
Posted
by E-K.
Last updated
.
Post marked as solved
3 Replies
1.7k Views
Hi there, I'm still new on learning how to code, so I'm seeking some guidance on this problem. I'm trying to make a page that shows a list of employees, I get an error reading 'Instance member 'data' cannot be used on type 'Employee'; did you mean to use a value of this type instead?' Below is the code. // AllEmployees.swift import SwiftUI struct AllEmployees: View { let names = data.firstName // Error shows on the next line ↓ @State private var data = Employee.data @State private var searchText = "" var body: some View { NavigationStack { List { ForEach(data.firstName, id: \.self) { name in NavigationLink { Text(data.firstName) } label: { Text(name) } } } .searchable(text: $searchText) .navigationTitle("All Employees") } } var searchResults: [String] { if searchText.isEmpty { return names } else { return names.filter { $0.contains(searchText) } } } } struct AllEmployees_Previews: PreviewProvider { static var previews: some View { NavigationView { AllEmployees() } } } Any help is appreciated :)
Posted
by E-K.
Last updated
.