paint-brush
Why I Cannot Add Variable to GeometryReader{} Closure Argumentby@siyang-ren
127 reads

Why I Cannot Add Variable to GeometryReader{} Closure Argument

by seanren96@gmail.com
seanren96@gmail.com HackerNoon profile picture

seanren96@gmail.com

@siyang-ren

I am a programmer focusing on Swift recently and have...

July 11th, 2020
Read on Terminal Reader
Read this story in a terminal
Print this story
Read this story w/o Javascript
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

A programmer has been using Javascrift for the past two years. He wants to use a GeometryReader{} to contain a view, so he can decide the position of the view base on the parent container property such as size, coordinates space, ect... But he gets an error like this: "Why I can not write variable inside a closure?" He then encountered another issue, Generic parameter`Content`could not be infered. So, I explicitly specify the arguments to fix this issue.

Companies Mentioned

Mention Thumbnail
Apple
Mention Thumbnail
Google
featured image - Why I Cannot Add Variable to GeometryReader{} Closure Argument
seanren96@gmail.com HackerNoon profile picture
seanren96@gmail.com

seanren96@gmail.com

@siyang-ren

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

About @siyang-ren
LEARN MORE ABOUT @SIYANG-REN'S
EXPERTISE AND PLACE ON THE INTERNET.

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:

image

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.

image

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.

image

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.

L O A D I N G
. . . comments & more!

About Author

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

TOPICS

THIS ARTICLE WAS FEATURED IN...

Permanent on Arweave
Read on Terminal Reader
Read this story in a terminal
 Terminal
Read this story w/o Javascript
Read this story w/o Javascript
 Lite
Hashnode
Learnrepo

Mentioned in this story

companies
X REMOVE AD