Learning to Read the Doc for AxisValueLabel

AxisValueLabel

I'm studying people's example code and comparing it to Apple's Chart documentation so that I can decipher the undecipherable, and ponder the depths of a mud pit.

The example code from Swift UI Recipies shows the chartYAxis block of code.  The AxisValueLabel has a closure block for its last parameter.  All the other parameters are optional or default values.


I don't know about you, but I've had a huge gap in my learning of SwiftUI - it is my inability to read the documentation of a Chart Construct such as AxisValueLabel and then convert that documentation into compiling code.

So let's get pedantic and deconstruct this AxisValueLabel.

First - note in the doc that there are 5 init methods and virtually nothing else.  This implies that this structure is some type of data holder that is initiated from other data and then does nothing with that data... well except make it appear on the Chart view.  This means we need to know which of the 5 methods we want to use.  And Second, if we study the methods most all of the parameters are optional (nil) or have a default value.  This non-obvious fact is the true key to understanding the structure.  Turns out this is a common pattern in SwiftUI Charts.  So many of the data-holding structures are going to have reasonable defaults and optional parameters - such that initializing them with empty arguments is going to work much of the time.

AxisValueLabel()   // works! - we get the default labels

In our example, however, the code is more complex than the simple default constructs.  So what is in the body of the AxisValueLabel's trailing closure - what parameter is it delivering?  There is no argument label with a trailing closure - so we have to parse each of the 5 initers and figure out which can have ZERO parameters and a trailing closure - that gets a Text() view.  A quick scan of the last argument to each of the 5 init() methods and we learn that 4 of the init() methods have verticalSpacing? as the last argument and only ONE has a "content" type.  If we look closer we see that it is actually "content: () -> Content".  This means it is a function accepting zero arguments and returning a Content type.  Clicking on that init() method in the docs leads to a detail page that describes the initializer where the content is a ViewBuilder function.  This is the one our example is using.

Now if you've been using SwiftUI for a little while you have been using ViewBuilders all over the place so you know how they work... or how you use them.  This specific block of code has a view wrapped in an if let block.  It is converting the value passed in the enclosing block up at AxisMarks, into an Interger.  I do not know why - but the people at Swift UI Receipes have attempted to explain it.


Also - I've yet to learn how to state in nglish words the meaning of the generic operations depicted in the AxisValueLabel declaration.  Something about the generic over Content... where content is a typical SwiftUI View.