Development Technology

Fixing Int, Identifiable, and ForEach in SwiftUI Beta 5

Fixing Int, Identifiable, and ForEach in SwiftUI beta 5.

I installed beta number 5 of SwiftUI this week and my app, ExerPlan, died an impressive death at the hands of deprecation and weirdly unhelpful error messages!

My app calls for a calendar-style interface. I use some code to iterate over the number of weeks a plan has been setup for, creating a row of UI for each week:

ForEach( 1…planDetail.planWeeks) { week in

Unfortunately, this stopped working in beta 5 as Int (which is what planWeeks is above) no longer conforms to Identifiable. It took me a while to understand the fix, but the code below eventually saw my project compile again.

ForEach( 1…planDetail.planWeeks, id: \.self) { week in

I have made sure that all relevant structs in my code conform to Identifiable, so this was a frustrating error, but at least the fix is easy.