swift syntax getting more complicated with nontrivial swiftUI examples; and Xcode errors are not always helpful, at least to me.

When I start with this, everything compiles fine:

struct ContentView1: View {
    @State var shouldShowRect = false
   
    var body: some View {
        GeometryReader { geometry in
            ZStack() {
                VStack() {
                    Text("Hello, World!")
                    Text("Hello, World again?")
                }
                if self.shouldShowRect {
                    Rectangle()
                }
            }
        }
    }
}


However when I add a let declaration inside GeometryReader, I get a compile error in xcode

struct ContentView2: View {
    @State var shouldShowRect = false
   
    var body: some View {
        GeometryReader { geometry in
            let dimension = geometry.size.width
            ZStack() {
                VStack() {
                    Text("Hello, World!")
                    Text("Hello, World again?")
                }
                if self.shouldShowRect {
                    Rectangle()
                }
            }
        }
    }
}

at line 6: Closure containing a declaration cannot be used with function builder 'ViewBuilder'


The GeometryReader block now needs to explicitly return a view, so I do this:

struct ContentView3: View {
    @State var shouldShowRect = false
   
    var body: some View {
        GeometryReader { geometry in
            let dimension = geometry.size.width
            let result0 = ZStack() {
                VStack() {
                    Text("Hello, World!")
                    Text("Hello, World again?")
                }
                if self.shouldShowRect {
                    Rectangle()
                }
            }
            return result0
        }
    }
}

This solves the problem at line6, but creates two new problems at lines 4 and 5

4: Function declares an opaque return type, but has no return statements in its body from which to infer an underlying type

5: Cannot convert return expression of type 'GeometryReader<_>' to return type 'some View'


I can silence the line4 problem by assigning GeometryReader to a variable and adding a return statement the end of the var body: closure.

replace line 05. with the following

        let result1 = GeometryReader { geometry in


add the following after line 17.

        return result1



but I'm left with two questions:
1. how can I silence the error still showing at line5 "cannot convert return expression..."

2. Why was the above change (let result1 = GeometryReader...) needed?


I have the impression that these two errors, reported at lines 4 & 5 are actually red herrings, and the real syntax problem is somewhere/something else.


any/all help greatly appreciated,


Mike

Replies

a bit in late.. but for next people...

I do calc let index = col + row * self.MaxCols outside, otherwise I got your message.





   let gr = GeometryReader { (geometry : GeometryProxy) in

            VStack() {

                ForEach(0..
                    HStack {

                        ForEach(0..<self.MaxCols) { (col: Int) -> GridCellView in

                            let index = col + row * self.MaxCols

                            return GridCellView(

                                w: (geometry.size.width / CGFloat(self.MaxCols)) - 25, // compensate inset..

                                titleAndText: firstLevelQuestions[index],

                                room: self.room,

                                troubleShootings: troubleShootings[index] )

                        }

                    }

                }

            }