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?

Answered by Gill-P in 827312022

Hey,

All my code is on GitHub; if you would like to have a look at the full code, all of the list view stuff is in ListView.swift and the courses in Courses_vDef.swift.
Here's the repository:
https://github.com/the-trumpeter/Timetaber-for-iWatch

Accepted Answer

Hey,

All my code is on GitHub; if you would like to have a look at the full code, all of the list view stuff is in ListView.swift and the courses in Courses_vDef.swift.
Here's the repository:
https://github.com/the-trumpeter/Timetaber-for-iWatch

This is not what is causing my app to crash! Sorry!

Another bit of my code hadn't been tested, and of course that was causing issues! Sorry!

Thanks for taking the time to consider my problem though!

[ if you really want to help me that much well the error is in TimeManager_fDef.swift on the GitHub repo, in the function getCurrentClass somewhere]

You did not give enough information on the crash. What message ? Where exactly ?

Your code is not complete to be tested:

  • roomOrBlank missing

I completed with:

    func roomOrBlank(course: Course) -> String {
        if course.room == "None" {
            return ""
        } else {
            return course.room
        }
    }
  • currentCourse missing
  • Rose, Lemon, White… missing: why not pass directly a Color as parameter instead of String ?

So I tested by removing those lines and it does not crash.

struct courseListView: View {
    var localcourse: Course
    var localtime: String
    
    func roomOrBlank(course: Course) -> String {
        if course.room == "None" {
            return ""
        } else {
            return course.room
        }
    }
    
    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)
    }
}

It does not seem the crash comes from removed line, as I tested with:

         .background(false ? Color(localcourse.colour).colorInvert(): nil)

without causing a crash

Anyway, you marked your own answer as correct. As it is not really an answer, we understand you closed the thread and don't need any more help.

Why is this causing my app to crash?
 
 
Q