Hackernoon logoWhy I Cannot Add Variable to GeometryReader{} Closure Argument by@siyang-ren

Why I Cannot Add Variable to GeometryReader{} Closure Argument

seanren96@gmail.com Hacker Noon profile picture

@siyang-renseanren96@gmail.com

I am a programmer focusing on Swift recently and have been using Javascrift for the past two years.

I want to use a GeometryReader{} to contain a view, so I can decide the position of the view base on the parent container property such as size, coordinates space, ect...

So, I have some code like this:

 GeometryReader {
                    geometry in
                    self.badgeSymbols
                        .scaleEffect(1.0 / 4.0, anchor: .top)
                        .position(x: geometry.size.width / 2.0,
                                  y: (3.0 / 4.0) * geometry.size.height)
                }
GeometryReader {
                    geometry in
                    var width = geometry.size.width ;
                    var height = geometry.size.height;
                    self.badgeSymbols
                        .scaleEffect(1.0 / 4.0, anchor: .top)
                        .position(x: width / 2.0,
                                  y: (3.0 / 4.0) * height)
                }

It should work just fine according to my knowledge.

How ever I get an error like this:

Encounter a strange error, I went to google the answer.

And following is my found:

1. Curly braces`{ ...body...}` after the GeometryReader is a syntax called closure. In this case, we call GeometryReader to initialize a GeometryReader instance. According to the Documentation of GeometryReader by Apple.

When we instantiate a type, and there is only one parameter, this parameter happens to be a function.Type, we can GeometryReader followed by {}function (Closure function).

2. Why I can not write variable inside a closure? Soon I realised I asked the wrong question.

Yes, I can write variable inside a closure.

I was writing a variable inside a return sentence. When there is no return keyword in the statements section, the whole section will be treated as a return statement. I can not write new variable inside the return statement.

So, I added a return keyword to teturn my view.

  GeometryReader {
                    geometry in
                    var width = geometry.size.width ;
                    var height = geometry.size.height;
                    self.badgeSymbols
                        .scaleEffect(1.0 / 4.0, anchor: .top)
                        .position(x: geometry.size.width / 2.0, y: (3.0 / 4.0) * geometry.size.height)
                    return Text("Position:x: \(geometry.size.width / 2.0) y: \((3.0 / 4.0) * geometry.size.height))")
                }

Now I encountered another issue, Generic parameter`Content`could not be infered. So, I explicitly specify the arguments to fix this issue. It goes like this:

GeometryReader<Text> {
                    geometry in
                    var width = geometry.size.width ;
                    var height = geometry.size.height;
                    self.badgeSymbols
                        .scaleEffect(1.0 / 4.0, anchor: .top)
                        .position(x: geometry.size.width / 2.0, y: (3.0 / 4.0) * geometry.size.height)
                    return Text("Position:x: \(geometry.size.width / 2.0) y: \((3.0 / 4.0) * geometry.size.height))")
                }

The reason being that, GeometryReader.Type has a generic parameter defined by Apple developer... It is stated like this:

@frozen struct GeometryReader<Content> where Content : View

Simple Explanation: I provide a type that conforms the View Protocol, the compilation will pass.

seanren96@gmail.com Hacker Noon profile picture
by seanren96@gmail.com @siyang-ren. I am a programmer focusing on Swift recently and have been using Javascrift for the past two years. Read my stories

Tags

Join Hacker Noon

Create your free account to unlock your custom reading experience.