1

How does this code snippet work? Maybe I am missing a fundamental point here but I always thought that constants are well, constant and the only properties you could modify were variables. How does this compile?

  extension MKPointAnnotation {
    static var example: MKPointAnnotation {
        let annotation = MKPointAnnotation()
        annotation.title = "Current Stay"
        annotation.subtitle = "Temporary Maryland Abode"
        annotation.coordinate = CLLocationCoordinate2D(latitude: 20, longitude: -70)
        
        return annotation
    }
}
Asperi
  • 228,894
  • 20
  • 464
  • 690
Sergio Bost
  • 2,591
  • 2
  • 11
  • 29
  • 3
    `title`, `subtitle`, and `coordinate` are properties of `MKPointAnnotation`. Those are declared as `var`s, so this works. – aheze Jan 23 '21 at 01:48
  • Ahh, thanks that makes sense. "let" is just a placeholder in this case then – Sergio Bost Jan 23 '21 at 02:00
  • 2
    MKPointAnnotation is a class, so though annotation is declared as a constant you can still modify properties of it. The let annotation only means a different object of MKPointAnnotation can’t be assigned to it. In case of struct that is declared as a constant you can’t modify properties of it. – user1046037 Jan 23 '21 at 02:20

0 Answers0