Sunday, December 15, 2024
HomeiOS Developmentios - SwiftUI utilizing computed property in ForEach assertion

ios – SwiftUI utilizing computed property in ForEach assertion


I’ve the next code in my SwiftUI view:

  @Binding var editorVM:EditorViewModel //<--- Is an @Observable class

   var physique: some View {
    HStack {
        Spacer()
            .body(width:25)
        
        ForEach(visualLayerItems, id: .id) { merchandise in //<-- this line offers construct errors
            ItemView(merchandise)
        }
       
        Spacer()
            .body(width: 25)
    }
    .padding()
}


  var visualLayerItems:[VisualLayerItem] {
    var gadgets:[VisualLayerItem] = []
    
    if let timelineModel = editorVM.timelineModel, let layerItems = timelineModel.visualLayerItems(inLayer: 0) {
        return layerItems
    }
   
    return gadgets
 }

However I get construct error :

  Can not convert worth of kind '[VisualLayerItem]' to anticipated argument kind 'Binding'

VisualLayerItem is a category that confirms to Identifiable. I perceive I’m utilizing a computed property and it appears to be like like ForEach is having bother with computed property and it most likely requires @State variable for the array. I can create an array and retailer it in a @State variable, however I believe if I make any adjustments in my timelineModel (saved in EditorViewModel which is @Observable), these adjustments wouldn’t be mechanically propagated to the view. What’s the proper method right here?

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments