UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

SOLVED: Day 16 -- Modifying program state

Forums > 100 Days of SwiftUI

Hi again

Friends, someone explain me why I need the self. in this example please.

The code seems to work pretty well without it.

struct ContentView: View {
    @State var tapCount = 0

    var body: some View {
        Button("Tap Count: \(tapCount)") {
            self.tapCount += 1
        }
    }
}

3      

Hi @gaspar-d,

You don't now, but when the tutorial was written you did. The Swift Community as a whole decided to change the need for using self in a closure in a struct - that it was actually a bug - and it has now been rectified. I do remember at some point that Paul had mentioned it, saying that perhaps by the time you are watching the video you won't kneed it aymore - but can't remember what day it was.

For moe info, you can google it. I pulled up this quick link: https://www.swiftbysundell.com/tips/implicit-capturing-of-self/

4      

Wow, now that you mention this I remember Paul saying it too.

Well better safe than sorry haha

Thanks for your reply

4      

BUILD THE ULTIMATE PORTFOLIO APP Most Swift tutorials help you solve one specific problem, but in my Ultimate Portfolio App series I show you how to get all the best practices into a single app: architecture, testing, performance, accessibility, localization, project organization, and so much more, all while building a SwiftUI app that works on iOS, macOS and watchOS.

Get it on Hacking with Swift+

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

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

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.