Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
294 views
in Technique[技术] by (71.8m points)

ios - Swift closure syntax using { ... in }

On the Apple SwiftUI Tutorial: Drawing Paths and Shapes, the following syntax is shown:

struct Badge: View {
    var body: some View {
        GeometryReader { geometry in
            Path { path in
            ...
            }
            .fill(Color.black)
        }
    }
}

I did not see the syntax in the Swift docs on structs and feel very confused.

Additionally, how might one know that geometry is "in" the GeometryReader statement, whatever that means?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

That has nothing to do with structs. That is closure syntax, where the variable name before in is the input argument to the closure.

In the context of SwiftUI, that closure is actually a ViewBuilder, which is a specific type of a function builder. You can read more about function readers and the other syntactic sugars that made SwiftUI possible here.

You can learn the type of the closure input argument(s) by option clicking on them in Xcode, just like any other variables. Or you can check the documentation of GeometryReader to learn what input arguments its closure accepts.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...