Home > Software design >  Cannot convert value of type 'Int' to expected argument type '()' when using onC
Cannot convert value of type 'Int' to expected argument type '()' when using onC

Time:07-01

I'm getting this 2 errors:

Cannot convert value of type 'Int' to expected argument type '()'
Type '()' cannot conform to 'Equatable'

when I try to add onChange to a Text in SwiftUI. I've also tried using indices instead of indices.count but I get the same error, except is [Int] instead of Int.

The minimal view I'm using for debugging this is:

struct MinPageView: View {
    @State var indices = [0]
    
    var body: some View {
        TabView() {
            ForEach(indices, id: \.self) {i in
                VStack{
                    Text("").onChange(of: indices.count){
                        print("Change")
                    }
                }.id(UUID())
                .onAppear{
                    indices.append(i 1)
                }
            }
        }
    }
}

CodePudding user response:

You forgot to disregard the count, with _ in.

Text("").onChange(of: indices.count) { _ in
  • Related