Home > Back-end >  SwiftUI ForEach Bindable List errors
SwiftUI ForEach Bindable List errors

Time:04-04

I would appreciate help with SwiftUI bindable lists. I read the following article and tried it on my app but I'm getting errors. enter image description here

When I try to fix some of the errors such as "remove ?", I get a new error saying I need to add the ?.

enter image description here

When I delete the default value ?? NotificationsDetail, I still get errors enter image description here

The Xcode build version is iOS15.

Does anyone know how to fix this? Thanks in advance.

CodePudding user response:

ForEach($notificationsViewModel.notifications?.notificationsDetails ?? [NotificationsDetail]()

Is confusing the type system, because one side of the ?? is a binding to an array of values, and the other side is an array of values. There's also an optional in your key path to make things more complicated.

Try to rearrange the NotificationsViewModel type so that it just surfaces a non-optional array instead of having all this optional mess at the view level. Is it really meaningful to have an optional notification property, or can an empty one be used instead? Do you need the separate notifications struct? Are you just modelling your data types directly from API responses? Perhaps you can make changes to your model types to make them easier to work with?

  • Related