TEAM LICENSES: Save money and learn new skills through a Hacking with Swift+ team license >>

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      

Hacking with Swift is sponsored by String Catalog.

SPONSORED Get accurate app localizations in minutes using AI. Choose your languages & receive translations for 40+ markets!

Localize My App

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.