Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save pauljohanneskraft/6b29e375477fb8a41017f26d15bd2e29 to your computer and use it in GitHub Desktop.
Save pauljohanneskraft/6b29e375477fb8a41017f26d15bd2e29 to your computer and use it in GitHub Desktop.
Handle value types as if they were references or references as if they were value types in Swift

Reference / Value Type

The Reference wrapper class for any Type in Swift makes it possible to handle objects from structs, as if they were classes (e.g. imitates behavior of Java references.)

The ValueType wrapper on the other hand mimics the behavior of structs for class objects.

You can also wrap the same type into its wrapper, but this doesn't make sense, even though it is possible.

class Reference<T> {
var value: T
init(_ value: T) {
self.value = value
}
}
struct ValueType<T> {
var value: T
init(_ value: T) {
self.value = value
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment