0

I would appreciate help with SwiftUI bindable lists. I read the following article and tried it on my app but I'm getting errors. https://www.swiftbysundell.com/articles/bindable-swiftui-list-elements/

First, the following View including the non-bindable ordinary ForEach list works fine without any errors

@ObservedObject var notificationsViewModel = NotificationsViewModel.shared
//NotificationsViewModel does a API call and puts the fetched data in the Notifications Model


var body: some View {

VStack {
ForEach(notificationsViewModel.notifications?.notificationsDetails ?? [NotificationsDetail]()) { notificationsDetail in 
---additional code here--- }
}   

Model below:

struct Notifications: Codable, Identifiable {
    
    let id = UUID()
    let numberOfNotifications: Int
    var notificationsDetails: [NotificationsDetail]

        enum CodingKeys: String, CodingKey {
            case numberOfNotifications = "number_of_notifications"
            case notificationsDetails = "notifications"
        }
}

struct NotificationsDetail: Codable, Identifiable, Equatable {
    let id: Int
    let notificationsCategoriesId: Int
    let questionsUsersName: String?
  
    enum CodingKeys: String, CodingKey {
        case id = "notifications_id"
        case notificationsCategoriesId = "notifications_categories_id"
        case questionsUsersName = "questions_users_name"
        
    }
}

When I try to change this ForEach to a bindable one, I start getting multiple errors.

ForEach($notificationsViewModel.notifications?.notificationsDetails ?? [NotificationsDetail]()) { $notificationsDetail in 
---additional code here using $notificationsDetail---}

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.

1 Answer 1

2
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?

Sign up to request clarification or add additional context in comments.

1 Comment

Hi jrturton. I made the notification property non-optional, and then it worked! Thanks for saving my week!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.