NEW: Start my new Ultimate Portfolio App course with a free Hacking with Swift+ trial! >>

WeSplit and closures - why is this closure different from that one?

Forums > 100 Days of SwiftUI

                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 do we need to prefix tipPercentages with self. inside the third closure, but not in the second? This has to be a language rule, but I'm just not getting it.

   

I do not think the self is necessary in this case

   

Hacking with Swift is sponsored by RevenueCat

SPONSORED Building and maintaining in-app subscription infrastructure is hard. Luckily there's a better way. With RevenueCat, you can implement subscriptions for your app in hours, not months, so you can get back to building your app.

Try it for free

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.