Puzzlin’ Code

Okay, here’s some SwiftUI code:

Section(header: Text("Tip amount")) {
                // right now we are in a trailing closure
                    Picker("Tip percentage",  selection: $tipPercentage) {
                    // and now we are inside a second closure
                        ForEach(0 ..< tipPercentages.count) { // here, tipPercentages doesn't need to be prefixed by 'self.'
                        // and now we are inside a third closure
                            Text("\(self.tipPercentages[$0])%") // here, tipPercentages *does* need to be prefixed
                        }
                    }.pickerStyle(SegmentedPickerStyle())
                }

Why, why, do I need to prefix tipPercentages with ‘self.‘ once we’re in the third closure but not inside the second or even the first? What’s magical about this situation? I know there’s something, but nobody is saying what.

Published by pirateguillermo

I play the bagpipes. I program computers. I support my family in their various endeavors, and I enjoy my wonderful life.

2 thoughts on “Puzzlin’ Code

  1. This blog posting is super narrow. Like 1/3 of the window width.

    SwiftUI code looks horrible. What happened to having a controller and just wiring it up?

    1. pirateguillermo – Santa Cruz, CA – I play the bagpipes. I program computers. I support my family in their various endeavors, and I enjoy my wonderful life.
      pirateguillermo says:

      I think I need to change the site theme. This just sucks for anything with decent real estate.

      As for controllers and InterfaceBuilder, well, it was perfect, so Apple fixed it. The New Way To Do It is all in code, and with an entirely different set of UI classes (although deep underneath, at the root of the hierarchy, it is still UIKit/AppKit). SwiftUI says, “Oh, we are all smart and we love closures so everything is a closure, that returns other closures.”

      I feel like maybe what happened was, some MIT graduates got senior enough at Apple that they were able to inflict their LISP viewpoint on everyone.

Leave a Reply