Skip to content

Instantly share code, notes, and snippets.

@iluuu1994
Created March 18, 2022 21:53
Show Gist options
  • Save iluuu1994/ffaff27dc1177bb130c973a6e7757072 to your computer and use it in GitHub Desktop.
Save iluuu1994/ffaff27dc1177bb130c973a6e7757072 to your computer and use it in GitHub Desktop.
Swift setter semantics with value types
import Foundation
class Foo {
var _numbers: [Int] = [42]
var numbers: [Int] {
get { _numbers }
set(value) {
print("Old: ", _numbers)
print("New: ", value)
}
}
}
let foo = Foo()
foo.numbers.append(43)
// http://online.swiftplayground.run/
// Old: [42]
// New: [42, 43]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment