Can someone explain the "self." keyword

I am having a hard time wrapping my head around the self.whatever code and why you use it. I just signed up in December for the courses and see it used in some of the lessons and I am having a hard time understanding it. The explanations from Chris are not really sinking in to me. When I think of self, it’s like something I have that I want to use instead of using someone else’s. Trying to use real life examples to wrap my head around it. If someone could explain “self” like I am a golden retriever, I would really appreciate it. Thanks in advance!

It would probably help if you included the code in question so that we can respond knowing what Chris was trying to explain and the context in which self was being used.

In Swift, the keyword self refers to the current instance of a class or struct. It can be used to access properties and methods of that current class or struct instance.

Let’s say that you have a property of a class that was declared as a variable. If you have a method (function) which declares a local variable of the same name and you wish to refer to the class property of that name then you add self.propertyName so that the compiler knows that you are accessing the class property rather than the local variable of the same name.

It is also used when you have a trailing closure in which you refer to the class or struct property. You need to include self as a prefix to the class or struct property inside that trailing closure.

If that still does not make sense then please include the code so that a targeted response can be made.

To complicate matters a little more there is also Self with a capital S but let’s not go there just yet.

Ok, that’s the explanation I needed. I’ll find the code snippet and paste it in here. Thanks for the response!

//
//  ContentView.swift
//  db-project-tracker2
//
//  Created by Paul Fulda on 1/17/25.
//

import SwiftUI
import SwiftData

struct ProjectListView: View {
    
    @State private var newProject: Project?
    @Query private var projects: [Project]
    var body: some View {
        
        NavigationStack {
            
            ZStack {
                LinearGradient(colors: [Color("Deep Purple"), Color("Blush Pink")], startPoint: .top, endPoint: .bottom)
                    .ignoresSafeArea()
                
                VStack (alignment: .leading) {
                    Text("Projects")
                        .font(Font.screenHeading)
                        .foregroundStyle(Color.white)
                    
                    ScrollView (showsIndicators: false) {
                        VStack (alignment: .leading, spacing: 26) {
                            
                            ForEach(projects) { p in
                                
                                NavigationLink {
                                    ProjectDetailView(project: p)
                                } label: {
                                    ProjectCardView(project: p)
                                }
                                .buttonStyle(PlainButtonStyle())
                            }
                        }
                    }
                }
                .padding()
                
                VStack {
                    Spacer()
                    HStack {
                        Button {
                            // Create new project
                            self.newProject = Project()
                        } label: {
                            ZStack {
                                Circle()
                                    .frame(width: 65)
                                    .foregroundStyle(Color.black)
                                Image("cross")
                            }
                        }
                        Spacer()
                        
                    }
                    .padding(.leading)
                }
            }
            
        }
        .sheet(item: $newProject) { project in
            AddProjectView(project: project)
                .presentationDetents([.fraction(0.2)])
        }
        
    }
}

#Preview {
    ProjectListView()
}

self.newProject = Project()

In the case of the example code you have it will make no difference whether self is used or not used but I suspect that Chris chose to add that simply because the Button has a trailing closure.

As Swift has evolved the requirement to use self is less and less important since the compiler can make assumptions that you are referring to the property of the struct. There may be circumstances where the complier will throw an error and tell you to add self to be explicit.

Ok, thanks for the explanations!

I should make a slight correction to the previous post I made. ie:

In actual fact I think Chris adds self to the variable newProject out of habit coming from a UIKit background.