-3

How this part o code works ? Cand someone explain more about self ?

import MapKit
class Artwork: NSObject, MKAnnotation {
  let title: String?
  let locationName: String
  let discipline: String
  let coordinate: CLLocationCoordinate2D

  init(title: String, locationName: String, discipline: String, coordinate: CLLocationCoordinate2D) {
  self.title = title
  self.locationName = locationName
  self.discipline = discipline
  self.coordinate = coordinate

  super.init()
  }
}
Roman Pokrovskij
  • 9,449
  • 21
  • 87
  • 142

1 Answers1

-2

Self is used when accessing the class that you are writing the code in.

class Cat {
    let catName = "My Cat"

    func name() {
        self.nameCat()
    }

    func nameCat() {
        let catName = "Sam"
        print(catName)
        print(self.catName)
    }
}

in this example when running name(), the terminal would print: "Sam" and then "My Cat". A variable without the "self" would have the value of the most 'recent' reference of that variable while a variable with "self" will reference the class. This also works with functions. You can run self.nameCat() to access the "nameCat" function inside the Cat class. Basically, "self" returns the instance of the class you are writing code in.

CentrumGuy
  • 576
  • 1
  • 4
  • 16