GiorgioE GiorgioE - 1 month ago 4
Swift Question

How can a operate with value of setter in structs?

I have this code of swift struct with getter and setter.
How that value of

got inside this setter? I don't know how it was defined.

struct Program {
var allResults : [String] = []
var items: Int {
get {
return allResults.count
set {
let result = String(newValue * 100) // what's that newValue, how did it get there?


It is called Shorthand Setter Declaration.

Cited from Swift 3 book:

If a computed property’s setter does not define a name for the new value to be set, a default name of newValue is used.

If you would like to have a better readable format, you could use this:

set (newItems) { //add your own variable named as you like
    let result = String(newItems * 100)