Access to struct properties

I completely understand all of the issues regarding the ability of a struct method to read and write the struct’s own properties. My question is, albeit, a beginner’s question. Assuming that, when an instance is created, the struct is assigned to a var, can entities outside the struct both read and write struct properties declared as vars?

@shogunkaramazov please can you take a look at this when you get a chance? Thanks!

1 Like

@chinggis Imagine you are writing a tutorial for the website and you want to change its title before sending it to the editing team like this:

// 1
struct Tutorial {
  var title: String
}

// 2
var tutorial = Tutorial(title: "What's new in Swift?")
print(tutorial.title) // "What's new in Swift?"
// 3
tutorial.title = "What's new in Swift 5?"
print(tutorial.title) // "What's new in Swift 5?"

Going over all of this step by step:

  1. You create a Tutorial structure with a title property.
  2. You write a new tutorial from scratch and set its title.
  3. You change the tutorial title before sending it to the tutorial editing team.

As you can see, you can both read and write the tutorial title. Now let’s see what actually happens with your tutorial under the hood during the editing phase:

// 1
struct Editor {
  let name: String
  var tutorial: Tutorial
  
  // 2
  mutating func editTutorial(newTitle: String) -> String {
    tutorial.title = newTitle
    return "\(tutorial.title) is edited by \(name)."
  }    
} 

// 3
var editor = Editor(name: "Cosmin", tutorial: tutorial)
let editedTutorial = editor.editTutorial(newTitle: "What's new in Swift 5.1?") 
print(tutorial.title) // "What's new in Swift 5?" 
print(editedTutorial) // "What's new in Swift 5.1? is edited by Cosmin."

Here is what happens in the above code:

  1. You create an Editor structure with both a name and a tutorial property.
  2. The editTutorial(newTitle:) mutating method changes the tutorial title during the editing process.
  3. The editing team assigns a new editor for your tutorial. He can both read and write the tutorial title but his changes are only saved in the tutorial draft and not published on the website in this case. This happens because you have defined Tutorial as a structure, so the editor is actually working only on a local copy of the tutorial instead of the tutorial itself.

In order to fix this, simply declare Tutorial as a class instead of a structure as follows and you are finally good to go:

class Tutorial {
  var title: String

  init(title: String) {
    self.title = title
  }
}

The previous block of code works now as expected in the first place and to begin with since the editor is actually working with a reference to the original tutorial this time:

var editor = Editor(name: "Cosmin", tutorial: tutorial)
let editedTutorial = editor.editTutorial(newTitle: "What's new in Swift 5.1?") 
print(tutorial.title) // "What's new in Swift 5.1?" 
print(editedTutorial) // "What's new in Swift 5.1? is edited by Cosmin."

Please let me know if you have any more questions or issues about the whole thing when you get a chance. Thank you!

1 Like

Quite clear and many thanks!
Tom

1 Like

This topic was automatically closed after 166 days. New replies are no longer allowed.