Created
March 18, 2022 21:53
-
-
Save iluuu1994/ffaff27dc1177bb130c973a6e7757072 to your computer and use it in GitHub Desktop.
Swift setter semantics with value types
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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