Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SwiftUI Xcode 11 beta 7 @Binding for collections is breaking preview

This was working until beta 7. It seems that when I want to pass one element in a collection as a binding to another view, the preview gets broken

I followed the release notes where it says @Binding no longer conforms to collection protocol comments are at the bottom

I have Catalina and xcode in beta 7

Parent:

struct SimpleStructure {
    var label: String
}

struct ContentView: View {
    @State private var myVariables = [SimpleStructure(label: "hello")]

    var body: some View {
        ChildView(myVariables: $myVariables)
    }
}

Child

struct ChildView: View {
    @Binding var myVariables: [SimpleStructure]
    var body: some View {
        List(myVariables.indexed(), id: \.1.label) {(index, variable) in
            GrandChildView(myVariable: self.$myVariables[index])
        }
    }
}

GrandChild

struct GrandChildView: View {
    @Binding var myVariable:SimpleStructure
    var body: some View {
        Text(myVariable.label)
    }
}

UPDATE:

Still an issue with Beta 8

like image 537
Facundo Luis Rodriguez Avatar asked Feb 03 '26 10:02

Facundo Luis Rodriguez


1 Answers

The release notes have a typo. You need to change:

var endIndex: Index { base.startIndex }

with

var endIndex: Index { base.endIndex }
like image 153
kontiki Avatar answered Feb 05 '26 03:02

kontiki



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!