Post

Replies

Boosts

Views

Activity

Using `@ObservedObject` in a function
No real intruduction for this, so I'll get to the point: All this code is on GitHub: https://github.com/the-trumpeter/Timetaber-for-iWatch But first, sorry; /* I got roasted, last time I posted; for not defining my stuff. This'll be different, but's gonna be rough; 'cuz there's lots and lots to get through: */ //this is 'Timetaber Watch App/Define (No expressions)/Courses_vDef.swift' on the GitHub: struct Course { let name: String let icon: String let room: String let colour: String let listName: String let listIcon: String let joke: String init(name: String, icon: String, room: String? = nil, colour: String, listName: String? = nil, listIcon: String? = nil, joke: String? = nil) { self.name = name self.icon = icon self.room = room ?? "None" self.colour = colour self.listName = listName ?? name self.listIcon = listIcon ?? (icon+".circle.fill") self.joke = joke ?? "" } } //this is 'Timetaber Watch App/TimeManager_fDef.swift' on the GitHub: func getCurrentClass(date: Date) -> Array<Course> { //returns the course in session depending on the input date //it is VERY long but //all you really need to know is what it returns: //basically: return [rightNow, nextUp] } /* I thought that poetry would be okay, But poorly thought things through: For I'll probably find that people online will treat my rhymes like spew. */ So into the question: I have a bunch of views, all (intendedly) watching two variables inside of a class: //Github: 'Timetaber Watch App/TimetaberApp.swift' class GlobalData: ObservableObject { @Published var currentCourse: Course = getCurrentClass(date: .now)[0] // the current timetabled class in session. @Published var nextCourse: Course = getCurrentClass(date: .now)[1] // the next timetabled class in session } ...and a bunch of views using them in different ways as follows: (Sorry, don't have the characters to define functions called in these) import SwiftUI //Github: 'Timetaber Watch App/Views/HomeView.swift' struct HomeView: View { @StateObject var data = GlobalData() var body: some View { //HERE: let icon = data.currentCourse.icon let name = data.currentCourse.name let colour = data.currentCourse.colour let room = roomOrBlank(course: data.currentCourse) let next = data.nextCourse VStack { //CURRENT CLASS Image(systemName: icon) .foregroundColor(Color(colour))//add an SF symbol element .imageScale(.large) .font(.system(size: 25).weight(.semibold)) Text(name) .font(.system(size:23).weight(.bold)) .foregroundColor(Color(colour)) .padding(.bottom, 0.1) //ROOM Text(room+"\n") .multilineTextAlignment(.center) .foregroundStyle(.gray) .font(.system(size: 15)) if next.name != noSchool.name { Spacer() //NEXT CLASS Text(nextPrefix(course: next)) .font(.system(size: 15)) Text(getNextString(course: next)) .font(.system(size: 15)) .multilineTextAlignment(.center) } }.padding() } } // Github: 'Timetaber Watch App/Views/ListView.swift' struct listTemplate: View { @StateObject var data = GlobalData() var listedCourse: Course = failCourse(feedback: "lT.12") var courseTime: String = "" init(course: Course, courseTime: String) { self.courseTime = courseTime self.listedCourse = course } var body: some View { let localroom = if listedCourse.room == "None" { "" } else { listedCourse.room } let image = if listedCourse.listIcon == "custom1" { Image(.paintbrushPointedCircleFill) } else { Image(systemName: listedCourse.listIcon) } HStack{ image .foregroundColor(Color(listedCourse.colour)) .padding(.leading, 5) Text(listedCourse.name) .bold() Spacer() Text(courseTime) Text(localroom).bold().padding(.trailing, 5) } .padding(.bottom, 1) .background(data.currentCourse.name==listedCourse.name ? Color(listedCourse.colour).colorInvert(): nil) //HERE } } struct listedDay: View { let day: Dictionary<Int, Course> var body: some View { let dayKeys = Array(day.keys).sorted(by: <) List { ForEach((0...dayKeys.count-2), id: \.self) { let num = $0 listTemplate(course: day[dayKeys[num]] ?? failCourse(feedback: "lD.53"), courseTime: time24toNormal(time24: dayKeys[num])) } } } } struct ListView: View { var body: some View { if storage.shared.termRunningGB && weekdayFunc(inDate: .now) != 1 && weekdayFunc(inDate: .now) != 7 { ScrollView { listedDay( day: getTimetableDay( isWeekA: getIfWeekIsA_FromDateAndGhost( originDate: .now, ghostWeek: storage.shared.ghostWeekGB ), weekDay: weekdayFunc(inDate: .now) ) ) } } else if !storage.shared.termRunningGB { Text("There's no term running.\nThe day's classes will be displayed here.") .multilineTextAlignment(.center) .foregroundStyle(.gray) .font(.system(size: 13)) } else { Text("No school today.\nThe day's classes will be displayed here.") .multilineTextAlignment(.center) .foregroundStyle(.gray) .font(.system(size: 13)) } } } //There's one more view but I can't fit it for characters. //On GitHub: 'Timetaber Watch App/Views/SettingsView.swift' So... THE FUNCTION: This function is called when changes are made that will affect the correct output of getCurrentClass. It is intended to reload the views and the current/next variables to reflect those changes.\ //GHub: 'Timetaber Watch App/StorageManager.swift' func reload() -> Void { @ObservedObject var globalData: GlobalData //this line is erroring, I don't know how to fix it. Is this even the best/proper way to do this? let courseData = getCurrentClass(date: .now) globalData.currentCourse = courseData[0] globalData.nextCourse = courseData[1] //Variable '_globalData' used by function definition before being initialized //that is the error appearing on those above two redefinitions. print("Setup done\n") } Thanks! -Gill
1
0
253
Mar ’25
Why is this causing my app to crash?
I'm developing an app for my watch and have a list of different classes (referred to as 'courses' to avoid confusion) that I take in school, eg: struct Course { let name: String let icon: String let room: String let colour: String let listName: String let listIcon: String init(name: String, icon: String, room: String? = nil, colour: String, listName: String? = nil, listIcon: String? = nil) { self.name = name self.icon = icon self.room = room ?? "None" self.colour = colour self.listName = listName ?? name self.listIcon = listIcon ?? icon+".circle.fill" } } // here's a few of my Course variables (there are lots more I've excluded) let MathsCourse = Course(name: "Maths", icon: "number", room: "FT5", colour: "Rose") let English6 = Course(name: "English", icon: "book.closed", room: "BT6", colour: "Lemon") let LunchPeriod = Course(name: "Lunch", icon: "fork.knife", room: "food", colour: "White") and I have designed a 'list view' of all the courses I have on whatever day it is. I used to define a different View for every course I defined but I merged the list data with the overall Course and replaced the Views with something that I should be able to call repeatedly and pass in a course to use, but unfortunately it's not working. (yes, I'm making a timetable app) This is the template for a 'list view' for a class: struct courseListView: View { var localcourse: Course var localtime: String var body: some View { HStack{ Image(systemName: localcourse.listIcon) .foregroundColor(Color(localcourse.colour)) .padding(.leading, 5) Text(localcourse.name) .bold() Spacer() Text(localtime) Text(roomOrBlank(course: localcourse)).bold().padding(.trailing, 5) } .padding(.bottom, 1) .background(currentCourse.name==localcourse.name ? Color(localcourse.colour).colorInvert(): nil) } } Then I should be able to programmatically work out what courses I have that day (I haven't scripted that bit yet), and compose a view containing all the courses: struct ListView: View { var body: some View { ScrollView { VStack(alignment: .leading) { courseListView(localcourse: MathsCourse, localtime: "10:00") // ^^^ note above line; I'll come back to this // the idea is I can repeat that for a bunch of courses: courseListView(localcourse: English6, localtime: "11:00") courseListView(localcourse: LunchPeriod, localtime: "12:00") } } } } Then be able to call all that in my @main: @main struct Timetaber_Watch_AppApp: App { var body: some Scene { WindowGroup { TabView{ HomeView() ListView() SettingsView() } .tabViewStyle(.carousel) .onAppear() { log() } } } } Unfortunately, each time I try to get a list view for a course, // if you need a reminder: courseListView(localcourse: MathsCourse, localtime: "10:00") ...even only calling it once causes my entire app to crash. Here's an excerpt from the crash report: Exception Type: EXC_BREAKPOINT (SIGTRAP) Exception Codes: 0x0000000000000001, 0x0000000180210194 Termination Reason: SIGNAL 5 Trace/BPT trap: 5 Terminating Process: exc handler [14932] Triggered by Thread: 0 Thread 0 Crashed:: Dispatch queue: com.apple.main-thread 0 libdispatch.dylib 0x180210194 _dispatch_once_wait.cold.1 + 28 1 libdispatch.dylib 0x1801db4f4 _dispatch_once_wait + 184 2 ??? 0x3400200d0 ??? 3 ??? 0x340020198 ??? 4 ... So... Anyone know what's happening?
3
0
265
Mar ’25
SOLVED: Thread 1: EXC_BREAKPOINT (code=1, subcode=0x10313ae30)
I'm trying to make a timetable app for my Apple Watch, and it has all actually been going pretty smoothly until this random error started showing up when I try to build my application. Here's a code snippet: // all on top level: class globalStorage { static let shared = globalStorage() // line 27 @State @AppStorage(runningKey) var termRunningGB = false @State @AppStorage(ghostWeekKey) var ghostWeekGB = false @State @AppStorage(startDateKey) var startDateGB = Date.now var currentCourse: Course = getCurrentClass(date: .now) } let storage = globalStorage.shared // &amp;lt;&amp;lt; ERRORING HERE (line // ... @main struct myApp: App { /* ... */ } Can anybody tell me what is happening? (And, of course, how to fix it?) Furthermore, upon removing the offending line (let storage = globalStorage.shared) (and replacing all callers of said variable with 'globalStorage.shared' to bypass the variable) the error has decided to settle on line 27, where i define the 'shared' thing in the class. [ I just went back to try more solutions, I have resolved it but forgot to give my solution here. Now I've forgotten how I fixed it. I do know that I moved currentCourse out of the class, that most likely was it I think.] All of this code is on GitHub: https://github.com/the-trumpeter/Timetaber-for-iWatch
1
0
312
Feb ’25